String Methods (upper, lower, replace…)

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

String Methods (upper, lower, replace…)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does 'hello'.upper() return?
A 'HELLO'
B 'Hello'
2. What does 'HELLO'.lower() return?
A 'Hello'
B 'hello'
3. What does 'abc'.replace('b', 'x') return?
A 'axc'
B 'abc'
4. Do string methods change the original string?
A No they return a new string
B Yes
5. What does ' hi '.strip() return?
A ' hi '
B 'hi'

Match the Pairs

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

Left columnRight column
1name = 'hello' name.Upper() AError: replace() needs 2 arguments
2'hello'.replace('l') BOk: Returns ['a','b','c']
3' hi '.strip() CError: Method names are lowercase
4'a,b,c'.split(',') DValid: Returns 'hi'
5print('hello'.upper()) EOutput: Hi
6print('hi'.replace('h','H')) FOutput: HELLO

Definitions

.upper()

Fill in the Blanks

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

1.
name = 'hello' print(name._____())
2.
name = 'HELLO' print(name._____())
3.
msg = 'I like cats' print(msg._______('cats', 'dogs'))
4.
text = ' hi ' print(text._____())
5.
csv = 'a,b,c' print(csv._____(','))

Spot the Error

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

Question 1
A name = 'hello'
B name.upper()
C print(name)
D print('Done')
Question 2
A name = 'hello'
B result = name.Upper()
C print('Result:', result)
D print('Done')
Question 3
A name = 'hello'
B result = upper(name)
C print('Result:', result)
D print('Done')
Question 4
A msg = 'hello'
B result = msg.replace('l')
C print('Result:', result)
D print('Done')
Question 5
A data = ' hi '
B cleaned = data.strip
C print('Cleaned:', cleaned)
D print('Done')

Answer Key

String Methods (upper, lower, replace…)
Section A: Quick Questions
Q1
A — 'HELLO'
Q2
B — 'hello'
Q3
A — 'axc'
Q4
A — No they return a new string
Q5
B — 'hi'
Section B: Match the Pairs
Q1
name = 'hello' name.Upper() → Error: Method names are lowercase
Q2
'hello'.replace('l') → Error: replace() needs 2 arguments
Q3
' hi '.strip() → Valid: Returns 'hi'
Q4
'a,b,c'.split(',') → Ok: Returns ['a','b','c']
Q5
print('hello'.upper()) → Output: HELLO
Q6
print('hi'.replace('h','H')) → Output: Hi
Section C: Definitions
Q1
.upper() — Returns the string in all uppercase letters.~~.lower()
Section D: Fill in the Blanks
Q1
name = 'hello' print(name.upper())
Q2
name = 'HELLO' print(name.lower())
Q3
msg = 'I like cats' print(msg.replace('cats', 'dogs'))
Q4
text = ' hi ' print(text.strip())
Q5
csv = 'a,b,c' print(csv.split(','))
Section E: Spot the Error
Q1
B — upper() returns a new string: must assign it back or print directly
Q2
B — Method names are lowercase: upper(), not Upper()
Q3
B — upper() is a string method: use name.upper(), not upper(name)
Q4
B — replace() needs two arguments: old and new
Q5
B — Missing parentheses: strip is a method, call it with strip()