← Python

String Formatting (.format())

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

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 journey and assign it to your class.

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 Alice' ✓
B. 'Hi {}'
{} 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. It's faster
B. Handles mixed types automatically ✓
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 ()
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?