Strings – Indexing: name[0], online

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

Strings – Indexing: name[0] in one minute

What students need to know

Index 0 is always the first character. 'python'[0] is 'p'. Negative indices count from the end. -1 is always the last character.

P=0, y=1, t=2, h=3, o=4, n=5. 'cat' has indices 0, 1, 2 — index 3 is out of range.

Mistakes students make

  • 'hello' has indices 0-4: index 5 is out of range.
  • First character is at index 0, not 1.
  • Strings are immutable: you can't change individual characters.
  • Index must be an integer, not a string.

Discover other journeys with this module

What students will learn in this module

Every question in Module 29

Quick questions

  1. What index is the first character of a string?A0B1
  2. What does 'Python'[0] return?A'y'B'P'
  3. What does 'Python'[5] return?A'n'B'o'
  4. What does 'hello'[-1] return?A'o'B'h'
  5. What does 'cat'[3] do?AReturns 't'BIndexError

Key terms

Students type the meaning of:

  • Index
  • string[0]
  • Negative index
  • IndexError

Match the pairs

word = 'cat'
word[3]
Match it to what it does
word = 'hello'
word[1] = 'a'
Match it to what it does
'python'[0]Match it to what it does
'hello'[-1]Match it to what it does
w = 'Python'
print(w[0])
Match it to what it does
w = 'Python'
print(w[-1])
Match it to what it does

Fill in the blanks

  1. name = 'Python'
    print(name[_])
  2. word = 'hello'
    print(word[__])
  3. name = 'Alice'
    print(___('Alice'))
  4. text = 'code'
    print(text[_])
  5. word = 'Python'
    print(word[_])

Spot the error

Question 1

Aname = 'hello'Bfirst = name[0]Clast = name[5]Dprint(first, last)

Question 2

Aname = 'hello'Bfirst = name[1]Cprint('First letter:', first)D# expects: h

Question 3

Aname = 'hello'Bprint('Before:', name)Cname[0] = 'H'Dprint('After:', name)

Question 4

Aname = 'hello'Bindex = '0'Cfirst = name[index]Dprint('First:', first)

Question 5

Aname = 'hello'Bfirst = name(0)Cprint('First letter:', first)Dprint('Done')