len() function, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
len() function in one minute
What students need to know
'Python' has 6 characters, so len() returns 6. The empty string has zero characters, so len('') returns 0.
Count each character: H-e-l-l-o = 5. H-i = 2 characters.
Mistakes students make
- Line 2 is missing parentheses. Should be: length = len(name).
- Line 2 puts the variable in quotes. Should be: len(word).
- Line 2 uses the wrong function name. Should be: len(message).
- Line 4 is missing quotes around Hello. Should be: len("Hello").
PREVIOUS · MODULE 8String Repetition
YOU ARE HERE · MODULE 9len() function
NEXT · MODULE 10F-String Formatting
What students will learn in this module
Every question in Module 9
Quick questions
- What is
len("Hello")?A5B6 - What is
len("Hi")?A2B3 - What is
len("Hello World")?A11B10 - Do spaces count in
len()?ANoBYes - After
name = "Alice", what islen(name)?A4B5
Key terms
Students type the meaning of:
len("Hello")len("Hello World")name = "Alice"len(name)
Match the pairs
len(Hello)Match it to what it doeslen('cat', 'dog')Match it to what it doesn = len('Python')Match it to what it doeslen('')Match it to what it doesprint(len('Hello'))Match it to what it doesw = 'cat' print(len(w))Match it to what it does
Fill in the blanks
length = ___("Hello")name = "Alice" result = len(____) print(result)
message = "Hi there" size = ___(message)
word = "Python" length = len(word) print(______)
text = "Hello World" count = len(____) print(count)
Spot the error
Question 1
Aname = "Alice"Blength = len nameCprint(length)
Question 2
Aword = "Python"Bsize = len("word")Cprint(size)
Question 3
Amessage = "Hello"Bcount = length(message)Cprint(count)
Question 4
Atext = "Hi there"Bsize = len(text)Cprint(size)Dnew_len = len(Hello)Eprint(new_len)
Question 5
Aname = "Bob"Bname_length = len(name)Cmessage = "Python Code"Dmsg_len = len messageEprint(msg_len)
