Formatting output and string methods
Programs should give clear, tidy output. The format() method lines up columns and controls decimal places, and string methods let you search and change text.
The format() method
name = "Ola"
score = 7.456
print("{} scored {:.2f}".format(name, score))
print("[{:>6}]".format("cat"))
print("[{:<6}]".format("cat"))
print("[{:5d}]".format(42))
Output:
Ola scored 7.46
[ cat]
[cat ]
[ 42]
Lining up a table
items = [["Pen", 1.2], ["Notebook", 3.75], ["Bag", 12.5]]
for item in items:
print("{:<10}{:>7.2f}".format(item[0], item[1]))
Output:
Pen 1.20
Notebook 3.75
Bag 12.50
Useful string methods
word = "Banana"
print(word.find("an"))
print(word.find("x"))
print(word.replace("a", "o"))
print(word.lower().count("a"))
print("2026".isdigit(), "Year10".isalpha())
Output:
1
-1
Bonono
3
True False
Characters and codes
code = ord("C")
print(code)
print(chr(code + 2))
print(round(3.14159, 2))
Output:
67
E
3.14
Write a line that prints the price 4.5 as £4.50.
- Use a placeholder with .2f for two decimal places.
- print("£{:.2f}".format(4.5))
Answer: £4.50
format() fills {} placeholders: < and > align, a number sets the width, .2f gives 2 decimal places and d a whole number. find() returns -1 if not found; replace(), isdigit(), isalpha(), upper() and lower() work on strings; ord() and chr() convert characters and codes.
Try two quick questions
See if it has sunk in. Answers are marked straight away.
2 more questions
Try each one, then tap to check.
What does this print?
print("{:.1f}".format(7.36))Show the answer
Answer: 7.4
{:.1f} rounds to 1 decimal place: 7.36 → 7.4.
What does this print?
print("{} scored {} goals".format("Ola", 5))Show the answer
Answer: Ola scored 5 goals
Each {} is filled in order: Ola, then 5.
Check you have got it
Answer 7 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.
- Pearson Edexcel GCSE Computer Science (1CP2) specification, topic 6.3.3, 6.4.1, 6.6.1
- Pearson Edexcel International GCSE Computer Science (4CP0) specification, topic 2.3.3, 2.4.1, 2.6.1
Checked by the GCSE-ready editorial team, 17 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.
What do exam command words like explain and evaluate mean? →