len() function

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

len() function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is len("Hello")?
A 5
B 6
2. What is len("Hi")?
A 2
B 3
3. What is len("Hello World")?
A 11
B 10
4. Do spaces count in len()?
A No
B Yes
5. After name = "Alice", what is len(name)?
A 4
B 5

Match the Pairs

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

Left columnRight column
1len(Hello) AOk: Returns 0
2len('cat', 'dog') BOutput: 3
3n = len('Python') CValid: n stores 6
4len('') DError: Missing quotes
5print(len('Hello')) EOutput: 5
6w = 'cat' print(len(w)) FError: Too many arguments

Definitions

len("Hello")

Fill in the Blanks

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

1.
length = ___("Hello")
2.
name = "Alice" result = len(____) print(result)
3.
message = "Hi there" size = ___(message)
4.
word = "Python" length = len(word) print(______)
5.
text = "Hello World" count = len(____) print(count)

Spot the Error

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

Question 1
A name = "Alice"
B length = len name
C print(length)
Question 2
A word = "Python"
B size = len("word")
C print(size)
Question 3
A message = "Hello"
B count = length(message)
C print(count)
Question 4
A text = "Hi there"
B size = len(text)
C print(size)
D new_len = len(Hello)
E print(new_len)
Question 5
A name = "Bob"
B name_length = len(name)
C message = "Python Code"
D msg_len = len message
E print(msg_len)

Answer Key

len() function
Section A: Quick Questions
Q1
A — 5
Q2
A — 2
Q3
A — 11
Q4
B — Yes
Q5
B — 5
Section B: Match the Pairs
Q1
len(Hello) → Error: Missing quotes
Q2
len('cat', 'dog') → Error: Too many arguments
Q3
n = len('Python') → Valid: n stores 6
Q4
len('') → Ok: Returns 0
Q5
print(len('Hello')) → Output: 5
Q6
w = 'cat' print(len(w)) → Output: 3
Section C: Definitions
Q1
len("Hello") — len() counts characters - len("Hello") gives 5.~~len("Hello World")
Section D: Fill in the Blanks
Q1
length = len("Hello")
Q2
name = "Alice" result = len(name) print(result)
Q3
message = "Hi there" size = len(message)
Q4
word = "Python" length = len(word) print(length)
Q5
text = "Hello World" count = len(text) print(count)
Section E: Spot the Error
Q1
B — Line 2 is missing parentheses. Should be: length = len(name)
Q2
B — Line 2 puts the variable in quotes. Should be: len(word)
Q3
B — Line 2 uses the wrong function name. Should be: len(message)
Q4
D — Line 4 is missing quotes around Hello. Should be: len("Hello")
Q5
D — Line 4 is missing parentheses. Should be: msg_len = len(message)