🔵 🔵 🔵


Primary

၊၊||၊|။

f'...{}...' ⚬ᵖʸ|Definition|1st|20251021011225-00-⌔

2. Lexical analysis — Python 3 documentation#formatted-string-literals

2.5.7. f-strings

Added in version 3.6.

Changed in version 3.7: The await and async for can be used in expressions within f-strings.

Changed in version 3.8: Added the debug specifier (=)

Changed in version 3.12: Many restrictions on expressions within f-strings have been removed. Notably, nested strings, comments, and backslashes are now permitted.

A formatted string literal or f-string is a string literal that is prefixed with ‘ f ’ or ‘ F ’. Unlike other string literals, f-strings do not have a constant value. They may contain replacement fields delimited by curly braces {}. Replacement fields contain expressions which are evaluated at run time. For example:

>>> who = 'nobody'
>>> nationality = 'Spanish'
>>> f'{who.title()} expects the {nationality} Inquisition!'
'Nobody expects the Spanish Inquisition!'

Any doubled curly braces ({{ or }}) outside replacement fields are replaced with the corresponding single curly brace:

>>> print(f'{{...}}')
{...}

Other characters outside replacement fields are treated like in ordinary string literals. This means that escape sequences are decoded (except when a literal is also marked as a raw string), and newlines are possible in triple-quoted f-strings:

>>> name = 'Galahad'
>>> favorite_color = 'blue'
>>> print(f'{name}:\t{favorite_color}')
Galahad:       blue
>>> print(rf"C:\Users\{name}")
C:\Users\Galahad
>>> print(f'''Three shall be the number of the counting
... and the number of the counting shall be three.''')
Three shall be the number of the counting
and the number of the counting shall be three.

Expressions in formatted string literals are treated like regular Python expressions. Each expression is evaluated in the context where the formatted string literal appears, in order from left to right. An empty expression is not allowed, and both lambda and assignment expressions := must be surrounded by explicit parentheses:

>>> f'{(half := 1/2)}, {half ﹡ 42}'
'0.5, 21.0'

Reusing the outer f-string quoting type inside a replacement field is permitted:

>>> a = dict(x=2)
>>> f"abc {a["x"]} def"
'abc 2 def'

Backslashes are also allowed in replacement fields and are evaluated the same way as in any other context:

>>> a = ["a", "b", "c"]
>>> print(f"List a contains:\n{"\n".join(a)}")
List a contains:
a
b
c

It is possible to nest f-strings:

>>> name = 'world'
>>> f'Repeated:{f' hello {name}' ﹡ 3}'
'Repeated: hello world hello world hello world'

Portable Python programs should not use more than 5 levels of nesting.

CPython implementation detail: CPython does not limit nesting of f-strings.

Replacement expressions can contain newlines in both single-quoted and triple-quoted f-strings and they can contain comments. Everything that comes after a # inside a replacement field is a comment (even closing braces and quotes). This means that replacement fields with comments must be closed in a different line:

>>> a = 2
>>> f"abc{a  # This comment  }"  continues until the end of the line
...       + 3}"
'abc5'

After the expression, replacement fields may optionally contain:

  • a debug specifier – an equal sign (=), optionally surrounded by whitespace on one or both sides;
  • a conversion specifier!s, !r or !a; and/or
  • a format specifier prefixed with a colon (:).

See the Standard Library section on f-strings for details on how these fields are evaluated.

As that section explains, format specifiers are passed as the second argument to the format() function to format a replacement field value. For example, they can be used to specify a field width and padding characters using the Format Specification Mini-Language:

>>> number = 14.3
>>> f'{number:20.7f}'
'          14.3000000'

Top-level format specifiers may include nested replacement fields:

>>> field_size = 20
>>> precision = 7
>>> f'{number:{field_size}.{precision}f}'
'          14.3000000'

These nested fields may include their own conversion fields and format specifiers:

>>> number = 3
>>> f'{number:{field_size}}'
'                   3'
>>> f'{number:{field_size:05}}'
'00000000000000000003'

However, these nested fields may not include more deeply nested replacement fields.

Formatted string literals cannot be used as docstrings, even if they do not include expressions:

>>> def foo():
...     f"Not a docstring"
...
>>> print(foo.__doc__)
None
See also:
  • PEP 498 – Literal String Interpolation
  • PEP 701 – Syntactic formalization of f-strings
  • str.format(), which uses a related format string mechanism.

Printed 2026-06-28.

(echo:: @ )

Link to original

Secondary

• • •