Python f-strings (formatted string literals)
Python f-strings (formatted string literals)
Summary
An f-string is a string literal with an f prefix that evaluates {expression} at runtime. After a colon you can add a format spec like {x:.2f}, after ! a conversion like {x!r}, and from Python 3.8 a trailing = for debugging output ({x=}). Write {{ and }} to get literal braces.
Draft. License: CC BY 4.0 (this summary). Sources: [S1] Python Software Foundation, "Lexical analysis: f-strings (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text); [S2] Python Software Foundation, "Format Specification Mini-Language (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text).
Example
name, total = "Ada", 1234.5
print(f"{name!r} owes ${total:,.2f}") # 'Ada' owes $1,234.50
print(f"{total=}") # total=1234.5
Facts
- id: F1 claim: Formatted string literals (prefix f or F) were added in Python 3.6 (PEP 498); expressions in braces are evaluated at runtime. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F2 claim: A format spec follows a colon, e.g. f'{price:.2f}' for two decimals, f'{n:,}' for thousands separators, f'{s:>10}' to right-align in 10 characters. confidence: medium date: 2026-11-02 sources: [S1, S2] license: CC BY 4.0 checked_on: 2026-11-02
- id: F3 claim: Conversions !r, !s and !a apply repr(), str() or ascii() before formatting. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F4 claim: Since Python 3.8, adding = after the expression (f'{x=}') prints the expression text, an equals sign and the value, handy for debugging. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F5 claim: Doubled braces {{ and }} produce literal braces. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F6 claim: Format specs can themselves contain replacement fields, e.g. f'{value:{width}.{precision}f}'. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F7 claim: Objects like datetime accept their own format codes in the spec, e.g. f'{d:%Y-%m-%d}'. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F8 claim: Python 3.12 (PEP 701) relaxed f-string parsing: expressions may reuse the enclosing quote type and contain backslashes and comments. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
Caveats
- Do not use f-strings to build SQL or shell commands from user input; use parameterized queries or argument lists.
- Code that must run on Python before 3.12 cannot reuse the same quote type inside the braces.
Pointers
- id: P1 answers: Full details and updates where: https://docs.python.org/3/reference/lexical_analysis.html#f-strings access: free, public web page
Source: https://docs.python.org/3/reference/lexical_analysis.html#f-strings · License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted), condensed and paraphrased; sources: Python Software Foundation, Python 3 documentation, Lexical analysis: f-strings (PSF License 2.0); restated in our own words; Python Software Foundation, Python 3 documentation, Format Specification Mini-Language (PSF License 2.0); restated in our own words
Cite this
iinventedlight Library. 2026. "Python f-strings (formatted string literals)." iinventedlight Library, iil:note:zszspfxq, version of 2026-11-02. https://iinventedlight.com/library/reference/python-docs/python-f-strings/. License: CC-BY-4.0. Summary of https://docs.python.org/3/reference/lexical_analysis.html#f-strings.
@misc{iil_note_zszspfxq,
author = {{iinventedlight Library}},
title = {{Python f-strings (formatted string literals)}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-02},
url = {https://iinventedlight.com/library/reference/python-docs/python-f-strings/},
note = {Permanent ID iil:note:zszspfxq. License: CC-BY-4.0. Summary of https://docs.python.org/3/reference/lexical\_analysis.html\#f-strings.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/reference/python-docs/python-f-strings.md.