Width and Alignment

f"{x:<10}"   # left, 10 cols
f"{x:>10}"   # right
f"{x:^10}"   # centre
f"{x:*>10}"  # fill with *

Numbers

f"{x:.2f}"     # 2 decimals
f"{x:,.0f}"    # thousands sep
f"{x:+.2f}"    # show sign
f"{x:.3e}"     # scientific
f"{x:.0%}"     # percentage

Integers

f"{n:05d}"     # zero-padded
f"{n:b}"       # binary
f"{n:o}"       # octal
f"{n:x}"       # hex

Dates

from datetime import date
today = date.today()
f"{today:%Y-%m-%d}"

Self-Debug

x = 42
print(f"{x=}")   # x=42 (Python 3.8+)

Practise this on PyForm — free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm →