Language Syntax

F string — Python syntax

Evaluate replacement fields at runtime and format their values. Double braces produce literal braces; conversions include !s, !r and !a. Python 3.12 relaxed several expression restrictions, including quote reuse. Assignment expressions and lambda expressions inside replacement fields need explicit parentheses.

Formal Syntax & Structure

python syntaxpython
f"{expression[!conversion][:format_spec]}"  # brackets denote optional parts, not literal text

Parameters & Attributes

NameTypeDescription
expressionexpressionReplacement field value.
conversions | r | aOptional conversion before formatting.
format_specformat specificationOptional formatting instruction.

Return Value

str

Value obtained by evaluating this expression; see description for context and constraints.

Working Example

Example Snippetpython
price=12.5qty=2print(f'{{total}} = {price*qty:.2f}')
Python Reference