×

python · level 2 · unit 3 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A tells an f-string how to show a number.

Inside an f-string's curly brackets, add a colon after the value, then a format spec. {price:.2f} shows price with exactly two , so 4.5 becomes 4.50. Money looks right at last.

This is for display only: 2.678 shows as 2.68, but the value in the box does not change.

{crowd:,} puts commas between the thousands, and {share:.0%} turns 0.25 into 25%. You can mix them: {total:,.2f} gives 1,234.50.

price = 4.5
print(f"£{price:.2f}")
print(f"{2.678:.2f}")
crowd = 52341
print(f"{crowd:,} fans")
share = 0.25
print(f"{share:.0%} of the pizza")
print(f"£{1234.5:,.2f}")

The f in .2f stands for fixed: a fixed number of decimal places.