Strings – Indexing: name[0], online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
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.
PREVIOUS · MODULE 28Functions – Default Arguments
YOU ARE HERE · MODULE 29Strings – Indexing: name[0]
NEXT · MODULE 30Strings – Slicing
What students will learn in this module
Every question in Module 29
Quick questions
- What index is the first character of a string?A0B1
- What does 'Python'[0] return?A'y'B'P'
- What does 'Python'[5] return?A'n'B'o'
- What does 'hello'[-1] return?A'o'B'h'
- What does 'cat'[3] do?AReturns 't'BIndexError
Key terms
Students type the meaning of:
Indexstring[0]Negative indexIndexError
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 doesw = '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
name = 'Python' print(name[_])
word = 'hello' print(word[__])
name = 'Alice' print(___('Alice'))text = 'code' print(text[_])
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')
