String Formatting (.format())

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

String Formatting (.format())

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does 'Hi {}'.format('Alice') return?
A 'Hi {}'
B 'Hi Alice'
2. What does {} represent in .format()?
A Curly braces to print
B A placeholder for a value
3. '{} + {} = {}'.format(2, 3, 5) returns?
A '2 + 3 = 5'
B '{} + {} = {}'
4. What's the advantage of .format() over + concatenation?
A Handles mixed types automatically
B It's faster
5. What does f'Hi {name}' do?
A Inserts the variable name's value
B Prints 'Hi {name}'

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1'{} and {}'.format('a') AError: Not enough arguments for {}
2'{name}'.format('Alice') BOutput: 1 + 2 = 3
3'{} is {}'.format('Bob', 15) COutput: Ali
4'{:.2f}'.format(3.14159) DError: Named placeholder needs keyword arg
5print('{} + {} = {}'.format(1,2,3)) EOk: Returns '3.14'
6print('{name}'.format(name='Ali')) FValid: Bob is 15

Definitions

{}

Fill in the Blanks

Complete each line of code by filling in the missing parts.

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

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A name = 'Alice'
B age = 12
C msg = 'Hello {}'.format
D print(msg)
Question 2
A name = 'Alice'
B age = 12
C msg = 'Hi (name), age (age)'.format(name='Alice', age=12)
D print(msg)
Question 3
A name = 'Alice'
B age = 12
C msg = f'Hi [name], age [age]'
D print(msg)
Question 4
A name = 'Alice'
B age = 12
C msg = 'Hello {}'.format()
D print(msg)
Question 5
A template = '(name) and (age)'
B name = 'Alice'
C msg = template.format(name='Alice', age=12)
D print(msg)

Answer Key

String Formatting (.format())
Section A: Quick Questions
Q1
B — 'Hi Alice'
Q2
B — A placeholder for a value
Q3
A — '2 + 3 = 5'
Q4
A — Handles mixed types automatically
Q5
A — Inserts the variable name's value
Section B: Match the Pairs
Q1
'{} and {}'.format('a') → Error: Not enough arguments for {}
Q2
'{name}'.format('Alice') → Error: Named placeholder needs keyword arg
Q3
'{} is {}'.format('Bob', 15) → Valid: Bob is 15
Q4
'{:.2f}'.format(3.14159) → Ok: Returns '3.14'
Q5
print('{} + {} = {}'.format(1,2,3)) → Output: 1 + 2 = 3
Q6
print('{name}'.format(name='Ali')) → Output: Ali
Section C: Definitions
Q1
{} — Placeholder in .format() that is replaced by the argument value.~~.format()
Section D: Fill in the Blanks
Q1
name = 'Alice' print('Hello {}'.format(name))
Q2
age = 15 print('Age: {}'.format( age))
Q3
name = 'Bob' print(f'{name} is here')
Q4
price = 9.99 print('{:.2f}'.format(price))
Q5
a = 5 b = 3 print(f'{a} + {b} = {a + b}')
Section E: Spot the Error
Q1
C — Missing parentheses and argument: .format(name) needs ()
Q2
C — Use curly braces {} for placeholders, not parentheses ()
Q3
C — f-strings use {curly braces}, not [square brackets]
Q4
C — One {} placeholder but no argument provided
Q5
A — Use curly braces {} for placeholders, not parentheses ()