Skip to content

string literal

In Python, a string literal is a piece of source code that represents a str value, written between matching single quotes ', double quotes ", or triple quotes ''' or """. The interpreter creates the corresponding string object when it evaluates the literal.

Single and double quotes are interchangeable. Triple-quoted literals can span multiple lines and keep their newlines as part of the string, which makes them a good fit for docstrings and long blocks of text.

A string literal can carry an optional, case-insensitive prefix that changes how the literal is interpreted:

  • r or R marks a raw string, so backslashes are kept as literal characters instead of starting an escape sequence.
  • b or B produces a bytes object instead of a str.
  • f or F makes an f-string, where expressions inside {...} are evaluated and inserted at runtime.
  • t or T makes a t-string, which follows the same syntax rules as an f-string but evaluates to a string.templatelib.Template object instead of a str. Its interpolations are evaluated eagerly, just like an f-string’s, but the static text and the interpolated values stay separate so your code can process them before combining them. T-strings were added in Python 3.14.
  • u or U has no effect and exists for backwards compatibility with older code.

Both cases are valid, but the lowercase forms are far more common in practice. You can combine r with b, f, or t to mix prefixes, as in rb"..." or fr"...".

Without an r prefix, the parser interprets escape sequences such as \n, \t, \xhh, and \N{name}. Unrecognized escapes are kept as-is and trigger a SyntaxWarning since Python 3.12.

Example

The snippet below shows several common forms of string literals:

Language: Python
>>> "Hello"
'Hello'

>>> 'It\'s Python'
"It's Python"

>>> """A triple-quoted string
... can span multiple lines."""
'A triple-quoted string\ncan span multiple lines.'

>>> r"C:\Users\name"
'C:\\Users\\name'

>>> name = "World"
>>> f"Hello, {name}!"
'Hello, World!'

>>> b"\x89PNG"
b'\x89PNG'

Each value is built directly from a string literal in the source code. The raw string keeps backslashes intact, the f-string substitutes the value of name, and the bytes literal produces a bytes object instead of a str.

Python's F-String: An Improved String Interpolation and Formatting Tool

Tutorial

Python's F-String for String Interpolation and Formatting

Python's f-strings provide a readable way to interpolate and format strings. They're readable, concise, and less prone to error than traditional string interpolation and formatting tools, such as the .format() method and the modulo operator (%). F-strings are also faster than those tools!

basics python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Sept. 19, 2026 • Reviewed by Leodanis Pozo Ramos