Lesson 16 / 47
f-strings & Formatting
Embed expressions in strings and control number formatting.
Expressions inside {}
f-strings evaluate any expression inside {} — including function calls and math.
name = "Ada"
score = 92.456
print(f"{name} scored {score:.1f}%")
print(f"Doubled: {score * 2:.0f}")
Output:
Ada scored 92.5% Doubled: 185
Format specifiers
Format specs after : control width, decimals, padding and commas — e.g. f"{n:,}" adds thousand separators.