← Python

String Formatting (.format())

.format() inserts values into a string by replacing {} placeholders

Understanding String Formatting (.format()) in Python

What it does: {} placeholders are filled in order by the .format() arguments. :.2f formats a float to 2 decimal places. {} is a placeholder — .format() fills it in with the argument. {} marks where the value from .format() gets inserted. Each {} is replaced by the matching argument: 2, 3, 5. You can't + a string and number directly — .format() converts them for you. In an f-string, {name} is replaced by the variable's value. {name} is replaced by 'Bo'.

Common mistakes: Missing parentheses and argument: .format(name) needs (). Use curly braces {} for placeholders, not parentheses ().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 31–40) and word search (Modules 21–40).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does 'Hi {}'.format('Alice') return?
'Hi Alice''Hi {}'
Matches
❌ '{} and {}'.format('a')
↓
Error: Not enough arguments for {}
Applying
Meanings
Term
{}
Meaning
Placeholder in .format() that is replaced by the argument value.
Practice
name = 'Alice'
Testing
Gaps
print('Hello '.format(name))
Check Answer
Spots
2age = 12
3msg = 'Hello {}'.format

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Meerkat' badge — Communicator, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does 'Hi {}'.format('Alice') return?
A. 'Hi {}'
B. 'Hi Alice' ✓
{} is a placeholder — .format() fills it in with the argument
2. What does {} represent in .format()?
A. Curly braces to print
B. A placeholder for a value ✓
{} marks where the value from .format() gets inserted
3. '{} + {} = {}'.format(2, 3, 5) returns?
A. '2 + 3 = 5' ✓
B. '{} + {} = {}'
Each {} is replaced by the matching argument: 2, 3, 5
4. What's the advantage of .format() over + concatenation?
A. Handles mixed types automatically ✓
B. It's faster
You can't + a string and number directly — .format() converts them for you
5. What does f'Hi {name}' do?
A. Inserts the variable name's value ✓
B. Prints 'Hi {name}'
In an f-string, {name} is replaced by the variable's value

Match the Pairs

'{} and {}'.format('a') → Error: Not enough arguments for {}
'{name}'.format('Alice') → Error: Named placeholder needs keyword arg
'{} is {}'.format('Bob', 15) → Valid: Bob is 15
'{:.2f}'.format(3.14159) → Ok: Returns '3.14'
print('{} + {} = {}'.format(1,2,3)) → Output: 1 + 2 = 3
print('{name}'.format(name='Ali')) → Output: Ali

Key Term

{}
Placeholder in .format() that is replaced by the argument value.~~.format()

Fill in the Blanks

1. name = 'Alice' print('Hello {}'.format(name))
2. age = 15 print('Age: {}'.format( age))
3. name = 'Bob' print(f'{name} is here')
4. price = 9.99 print('{:.2f}'.format(price))
5. a = 5 b = 3 print(f'{a} + {b} = {a + b}')

Spot the Error

Question 1
A name = 'Alice'
B age = 12
C msg = 'Hello {}'.format
D print(msg)
Line C — Missing parentheses and argument: .format(name) needs ()
Question 2
A name = 'Alice'
B age = 12
C msg = 'Hi (name), age (age)'.format(name='Alice', age=12)
D print(msg)
Line C — Use curly braces {} for placeholders, not parentheses ()
Question 3
A name = 'Alice'
B age = 12
C msg = f'Hi [name], age [age]'
D print(msg)
Line C — f-strings use {curly braces}, not [square brackets]
Question 4
A name = 'Alice'
B age = 12
C msg = 'Hello {}'.format()
D print(msg)
Line C — One {} placeholder but no argument provided
Question 5
A template = '(name) and (age)'
B name = 'Alice'
C msg = template.format(name='Alice', age=12)
D print(msg)
Line A — Use curly braces {} for placeholders, not parentheses ()

Student with a class code? Join your class →