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 textParameters & Attributes
| Name | Type | Description |
|---|---|---|
| expression | expression | Replacement field value. |
| conversion | s | r | a | Optional conversion before formatting. |
| format_spec | format specification | Optional 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}')