len() function, online

Six activities your students do on their own devices. See who's finished, and their scores.

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").

Discover other journeys with this module

What students will learn in this module

Every question in Module 9

Quick questions

  1. What is len("Hello")?A5B6
  2. What is len("Hi")?A2B3
  3. What is len("Hello World")?A11B10
  4. Do spaces count in len()?ANoBYes
  5. After name = "Alice", what is len(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 does
len('cat', 'dog')Match it to what it does
n = len('Python')Match it to what it does
len('')Match it to what it does
print(len('Hello'))Match it to what it does
w = 'cat'
print(len(w))
Match it to what it does

Fill in the blanks

  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

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)