Lesson 16 / 49
f-strings & Formatting
Embed expressions in strings with f-strings and format numbers using width, precision and separators.
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.