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:
rorRmarks a raw string, so backslashes are kept as literal characters instead of starting an escape sequence.borBproduces abytesobject instead of astr.forFmakes an f-string, where expressions inside{...}are evaluated and inserted at runtime.torTmakes a t-string, which is similar in syntax to an f-string but defers evaluation. T-strings were added in Python 3.14.uorUhas 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:
>>> "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.
Related Resources
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!
For additional information on related topics, take a look at the following resources:
- What Are Python Raw Strings? (Tutorial)
- Strings and Character Data in Python (Tutorial)
- A Guide to Modern Python String Formatting Tools (Tutorial)
- How to Format Floats Within F-Strings in Python (Tutorial)
- How to Convert Bytes to Strings in Python (Tutorial)
- Python 3's F-Strings: An Improved String Formatting Syntax (Course)
- Python's F-String for String Interpolation and Formatting (Quiz)
- Python Raw Strings (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
- Formatting Python Strings (Course)
- A Guide to Modern Python String Formatting Tools (Quiz)
- Formatting Floats Inside Python F-Strings (Course)
- Format Floats Within F-Strings (Quiz)
- How to Convert Bytes to Strings in Python (Quiz)