Start free

GCSE · 6 exercises

Python formatting

An f-string puts values into text: f"{name} is {age}". Inside the braces, a format after a colon controls how the value looks.

:.2f gives two decimal places, :5 makes room for five characters, :>5 lines up to the right, :05 fills with zeros.

price = 3.5
print(f"Total: £{price:.2f}")
print(f"{7:03}")  # 007

Watch for: round() can drop a trailing zero (3.50 becomes 3.5); :.2f always shows two places.

Exercises

  1. Receipt lineRead an item, its price and a quantity, and print a receipt line with the totalNovice
  2. PercentageRead a score and the maximum score, and print the score as a percentage with oneNovice
  3. Clock timeRead hours and minutes and print the time as HH:MM, with leading zeros.Novice
  4. Date formatRead a day, a month and a year, and print the date as DD.MM.YYYY.Novice
  5. Big moneyRead an amount of money and print it with a comma between thousands and exactlyNovice
  6. Score table rowRead a name and a score and print them as one row of a table: the name left-alignedNovice
Start with “Receipt line”