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.

Practise this lesson free (7 questions)

The format() method

Each {} in a string is a placeholder, filled in order by the values in format(). Inside a placeholder, after a colon: < left-aligns, > right-aligns, then the width, then .2f for a number with 2 decimal places, or d for a whole number.
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

Fixed widths make columns line up neatly:
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

find() gives the position of the first match, or -1 if there is none. replace() swaps every match. isdigit() and isalpha() check what a string contains.
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

ord() gives a character's code number; chr() turns a code back into a character. round(x, n) rounds to n decimal places.
code = ord("C")
print(code)
print(chr(code + 2))
print(round(3.14159, 2))
Output:
67
E
3.14
Worked example

Write a line that prints the price 4.5 as £4.50.

  1. Use a placeholder with .2f for two decimal places.
  2. print("£{:.2f}".format(4.5))

Answer: £4.50

Key idea

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.

Quick check

Try two quick questions

See if it has sunk in. Answers are marked straight away.

Extra practice

2 more questions

Try each one, then tap to check.

  1. 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.

  2. 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.

Video for this lesson coming soon. Subscribe on YouTube

Sources and references

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? →

Related lessons