Statement

f-strings

An f-string puts variables and expressions straight into text with {curly braces}, and can format numbers, dates and widths.

BeginnerEvery example below runs in your browser. Change the code and press Run.

f"text {expression} text" f"{expression:format_spec}" f"{expression=}"

Put the letter f before a string and you can write any Python expression inside {}. When the line runs, Python replaces each {...} with the value, turned into text.

After a colon you can add a format spec: {price:.2f} shows two decimals, {n:,} adds thousands separators, {share:.1%} shows a percentage and {name:<10} pads to 10 characters. Adding = (as in {total=}) prints the expression and its value, which is great for debugging.

To show a real curly brace, double it: {{ and }}. Before Python 3.12 you also cannot reuse the string's own quote character inside the braces, so use the other kind of quote for dictionary keys.

Return value

A normal string (str).

Examples

1Insert variables

Pythoneditable
name = "Hana"
city = "Kyoto"
age = 29
print(f"{name} is {age} and lives in {city}.")
print(f"Next year {name} will be {age + 1}.")
Output
Hana is 29 and lives in Kyoto.
Next year Hana will be 30.

2Format numbers

Pythoneditable
price = 1234.5
share = 0.1875
print(f"Price: {price:.2f} EUR")
print(f"Big: {1250000:,}")
print(f"Share: {share:.1%}")
print(f"Code: {7:03d}")
Output
Price: 1234.50 EUR
Big: 1,250,000
Share: 18.8%
Code: 007

3Line up a little table

Pythoneditable
items = [("Rent", 850), ("Food", 320.5), ("Phone", 25)]
for label, amount in items:
    print(f"{label:<8}{amount:>9.2f}")
Output
Rent       850.00
Food       320.50
Phone       25.00

<8 pads on the right to 8 characters; >9 aligns to the right in 9 characters.

4Debug with =

Pythoneditable
width = 4
height = 2.5
print(f"{width=}, {height=}")
print(f"{width * height=}")
Output
width=4, height=2.5
width * height=10.0

5Dates, dictionaries and braces

Pythoneditable
from datetime import date

d = date(2026, 7, 4)
user = {"name": "Iris", "points": 1520}
print(f"Date: {d:%d/%m/%Y}")
print(f"{user['name']} has {user['points']:,} points")
print(f"Use {{name}} as a placeholder")
Output
Date: 04/07/2026
Iris has 1,520 points
Use {name} as a placeholder

The dictionary key uses single quotes because the f-string itself uses double quotes.

Common mistakes

Forgetting the f

Without the f prefix, the braces are printed as they are.

Pythoneditable
total = 42
print("Total: {total}")
print(f"Total: {total}")
Output
Total: {total}
Total: 42

Rounding with round() instead of a format spec

round(2.5, 2) gives 2.5, not 2.50. To always show two decimals, format the number: f"{x:.2f}".

Pythoneditable
x = 2.5
print(round(x, 2))
print(f"{x:.2f}")
Output
2.5
2.50

Questions people ask

How do I format a number with 2 decimal places?

f"{value:.2f}". For thousands separators too: f"{value:,.2f}".

f-string vs format() vs %?

All three work. f-strings are the newest, shortest and most readable. str.format() is useful when the template is stored apart from the values.

Can I write a multi-line f-string?

Yes, with triple quotes: f"""Name: {name}""". Each line can contain {} fields.

How do I put a variable width in an f-string?

Nest braces in the spec: f"{name:<{width}}".

Spotted a mistake on this page? Tell us