CKTIS / Python
Formatted string literals
An f-string evaluates replacement fields at runtime and inserts their formatted values into text.
Version: Python 3.14; repository image, production runtime not attested
Syntax
f"{value!r:>12}"An f-string evaluates replacement fields at runtime and inserts their formatted values into text.
A field may contain an expression, a conversion such as !r, and a format specification after a colon. The width and zero padding in 03d apply to the integer when formatting, leaving its value intact. Doubling braces writes literal braces. The conversion !r calls repr before formatting, so the diagnostic includes quotes around the string.
The equals form displays the expression text as well as its value, useful in diagnostics. Format specifications depend on the value’s type: applying the integer code d to a string raises ValueError.
Examples
amount = 7
label = "oak"
print(f"{{{label}}}: {amount:03d}")
print(f"{label=!r}")
try:
print(f"{label:d}")
except ValueError as error:
print(type(error).__name__)
Result
{oak}: 007
label='oak'
ValueErrorThe equals form displays the expression text as well as its value, useful in diagnostics. Format specifications depend on the value’s type: applying the integer code d to a string raises ValueError.