Python f-strings — Formatted String Literals Explained

f-strings let you embed expressions directly inside string literals with f'{value}' syntax. Learn formatting specs, and how they compare to .format() and %.

Published September 18, 2026

An f-string is a string prefixed with f that evaluates expressions inside curly braces: f'Hello {name}'. They are the fastest and most readable way to format text in Python 3.6+.

The concept

Anything inside {} is a Python expression evaluated at runtime and converted to a string. You can add a format spec after a colon, such as {price:.2f} for two decimal places.

In 3.8+ you can write {value=} to print both the expression and its value, which is handy for debugging.

Formatting in action

name = 'Ada'; price = 1234.5
f'Hi {name}, total {price:,.2f}'
'Hi Ada, total 1,234.50'
f'{name=}'
"name='Ada'"

Practical examples

Expressions and methods

f'{name.upper()} has {len(name)} letters'

Number formatting

f'{0.256:.1%}'   # '25.6%'
f'{42:05d}'      # '00042'
f'{255:#x}'      # '0xff'

Alignment

f'{"left":<8}|{"right":>8}|{"mid":^8}|'

Dates

from datetime import datetime
f'{datetime.now():%Y-%m-%d}'

Common mistakes

  • Using the same quote type inside the braces before Python 3.12.
  • Putting untrusted user input in the format spec.
  • Forgetting the f prefix and printing the braces literally.
  • Using f-strings for SQL — use parameterised queries.

Key takeaways

  • f'...{expr:spec}...' formats inline.
  • {x=} is a debugging shortcut.
  • Double the braces {{ }} to print literal braces.

FAQ

f-strings vs format()?

f-strings are shorter and faster; format() is useful when the template is a variable.

Can I use f-strings in logging?

You can, but logging's lazy %s style avoids formatting when the message is filtered out.

More Python articles