โ† All cheat sheets

Python f-strings and formatting cheat sheet โœจ

Putting values in text, decimal places, padding, commas, percentages and dates. Every snippet runs in your browser, and the page prints on one sheet.

The basics

name = "Ada"
age = 36
print(f"{name} is {age}")
print(f"Next year: {age + 1}")
print(f"{name.upper()}!")

Put f before the quotes and any expression inside {}.

Decimal places

price = 3.14159
print(f"{price:.2f}")
print(f"{price:.0f}")
print(f"ยฃ{1234.5:,.2f}")

.2f is two decimal places. A comma adds thousands separators.

Percentages

rate = 0.0725
print(f"{rate:.1%}")
print(f"{3 / 8:.0%}")

% multiplies by 100 and adds the sign.

Padding and alignment

for item, qty in [("Tea", 2), ("Biscuits", 12)]:
    print(f"{item:<10}|{qty:>4}|")
print(f"{'mid':^9}|")
print(f"{7:03}")

< left, > right, ^ centre, then the width. 03 pads with zeros.

Debugging with =

x = 6
y = 7
print(f"{x=} {y=} {x * y=}")

Adding = shows the expression and its value.

Dates

from datetime import date
d = date(2026, 12, 25)
print(f"{d:%A %d %B %Y}")
print(f"{d:%d/%m/%y}")

Date format codes work inside f-strings.

Start the free lessons More cheat sheets