---
title: "Python f-strings (formatted string literals)"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
source_license: "PSF License 2.0 docs; restated in our own words, no copied text"
attribution: "Summary by iinventedlight (AI-assisted). Source: Python Software Foundation, \"Lexical analysis: f-strings (Python 3 docs)\" (PSF License 2.0 docs; restated in our own words, no copied text)."
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
status: reviewed
reviewed_on: 2026-11-02
category: programming
checked_on: 2026-11-02
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
source_license: "PSF License 2.0 (Python docs); restated in our own words, no copied text"
attribution: "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"
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
category: programming
tags: programming, python, strings, formatting
checked_on: 2026-11-02
sources:
  - id: S1
    title: "Lexical analysis: f-strings (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/reference/lexical_analysis.html#f-strings
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, Lexical analysis: f-strings (PSF License 2.0); restated in our own words"
  - id: S2
    title: "Format Specification Mini-Language (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/library/string.html#formatspec
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, Format Specification Mini-Language (PSF License 2.0); restated in our own words"
---
# 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
```python
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
