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}") # 007Watch for: round() can drop a trailing zero (3.50 becomes 3.5); :.2f always shows two places.
Exercises
- Receipt lineRead an item, its price and a quantity, and print a receipt line with the totalNovice
- PercentageRead a score and the maximum score, and print the score as a percentage with oneNovice
- Clock timeRead hours and minutes and print the time as HH:MM, with leading zeros.Novice
- Date formatRead a day, a month and a year, and print the date as DD.MM.YYYY.Novice
- Big moneyRead an amount of money and print it with a comma between thousands and exactlyNovice
- Score table rowRead a name and a score and print them as one row of a table: the name left-alignedNovice