Home / Learn / Code for makers

Python f-strings: format numbers, decimals, padding and dates

A Python f-string formatting a current in milliamps to one decimal place, a power with a thousands separator and a percentage, with the printed output
Beginner

An f-string is a string with an f in front, where anything in curly braces is worked out and put into the text. They're the quickest, clearest way to build messages and format numbers in Python. Every example below was run on Python 3.13, and the output shown is exactly what it printed.

The basics

name = "LED"
current = 0.0204
print(f"{name} current: {current} A")
print(f"{name} current: {current * 1000:.1f} mA")
volts = 3.3
r = 150
print(f"{volts} V across {r} Ω gives {volts / r * 1000:.2f} mA")
LED current: 0.0204 A
LED current: 20.4 mA
3.3 V across 150 Ω gives 22.00 mA

Anything valid in Python can go in the braces, including sums and function calls. After the expression, a colon starts the format spec, which controls how it looks. .1f means a fixed-point number with 1 decimal place.

Numbers

power = 1234567.891
print(f"{power:,.2f} W")
print(f"{power:_.0f}")
print(f"{0.256:.1%}")
print(f"{42:05d}")
print(f"{255:x} {255:X} {255:#x} {5:08b}")
print(f"{1500000:.3e}")
1,234,567.89 W
1_234_568
25.6%
00042
ff FF 0xff 00000101
1.500e+06
SpecMeaning
.2f2 decimal places
, or _thousands separator
.1%multiply by 100 and add %, 1 decimal place
05dwhole number, padded with zeros to 5 characters
x, X, #xhexadecimal, upper case, with 0x
08bbinary, padded to 8 bits: handy for registers
.3escientific notation

Rounding surprises

print(f"{2.675:.2f}")
print(f"{2.5:.0f} {3.5:.0f}")
2.67
2 4

2.675 can't be stored exactly in binary; it's really 2.67499999..., so it rounds down. And exact halves round to the nearest even number ("banker's rounding"), so 2.5 becomes 2. That's normal for floating point, not a bug. If you need exact decimal money sums, use Python's decimal module.

Padding and alignment: tidy tables

for part, qty in [("resistor", 120), ("LED", 8), ("capacitor", 35)]:
    print(f"|{part:<10}|{qty:>5}|")
print(f"|{'centre':^12}|")
print(f"|{'x':*^9}|")
|resistor  |  120|
|LED       |    8|
|capacitor |   35|
|   centre   |
|****x****|

< aligns left, > right and ^ centre, followed by the width. A character before the arrow sets what fills the gap.

The = shortcut for debugging

temp = 21.456
print(f"{temp=}")
print(f"{temp = :.1f}")
temp=21.456
temp = 21.5

Adding = prints the expression as well as its value, which saves typing when you're tracking down a bug. It needs Python 3.8 or later.

Other handy tricks

width = 8
temp = 21.456
print(f"[{3.14159:{width}.2f}]")
reading = {"temp": 21.5}
print(f"Temp: {reading['temp']} °C")
print(f"{{braces}} and {temp:.0f}")
[    3.14]
Temp: 21.5 °C
{braces} and 21
  • Format specs can contain their own braces, so the width can come from a variable.
  • Use the other kind of quote inside the braces. From Python 3.12 you can reuse the same quotes, but older versions give a SyntaxError, so mixing them is the safe habit.
  • Double the braces, {{ and }}, to print a literal brace.

Dates and times

from datetime import datetime
t = datetime(2026, 11, 21, 23, 5, 9)
print(f"{t:%Y-%m-%d %H:%M:%S}")
print(f"{t:%d/%m/%Y}")
2026-11-21 23:05:09
21/11/2026

Dates take the same codes as strftime. Year-month-day order sorts correctly as text, which makes it the best choice for log files and file names.

Older styles you'll still see

print("{:.2f} mA".format(20.4))
print("%.2f mA" % 20.4)
20.40 mA
20.40 mA

Both still work and you'll meet them in older code and tutorials. F-strings, added in Python 3.6, are usually easier to read.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.