Strings – Indexing: name[0]

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

Strings – Indexing: name[0]

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What index is the first character of a string?
A 0
B 1
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?
A Returns 't'
B IndexError

Match the Pairs

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

Left columnRight column
1word = 'cat' word[3] AOk: Returns last char 'o'
2word = 'hello' word[1] = 'a' BValid: Returns 'p'
3'python'[0] COutput: P
4'hello'[-1] DOutput: n
5w = 'Python' print(w[0]) EError: Index 3 out of range
6w = 'Python' print(w[-1]) FError: Strings are immutable

Definitions

Index

Fill in the Blanks

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

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

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

Question 1
A name = 'hello'
B first = name[0]
C last = name[5]
D print(first, last)
Question 2
A name = 'hello'
B first = name[1]
C print('First letter:', first)
D # expects: h
Question 3
A name = 'hello'
B print('Before:', name)
C name[0] = 'H'
D print('After:', name)
Question 4
A name = 'hello'
B index = '0'
C first = name[index]
D print('First:', first)
Question 5
A name = 'hello'
B first = name(0)
C print('First letter:', first)
D print('Done')

Answer Key

Strings – Indexing: name[0]
Section A: Quick Questions
Q1
A — 0
Q2
B — 'P'
Q3
A — 'n'
Q4
A — 'o'
Q5
B — IndexError
Section B: Match the Pairs
Q1
word = 'cat' word[3] → Error: Index 3 out of range
Q2
word = 'hello' word[1] = 'a' → Error: Strings are immutable
Q3
'python'[0] → Valid: Returns 'p'
Q4
'hello'[-1] → Ok: Returns last char 'o'
Q5
w = 'Python' print(w[0]) → Output: P
Q6
w = 'Python' print(w[-1]) → Output: n
Section C: Definitions
Q1
Index — The position number of a character in a string, starting at 0.~~string[0]
Section D: Fill in the Blanks
Q1
name = 'Python' print(name[0])
Q2
word = 'hello' print(word[-1])
Q3
name = 'Alice' print(len('Alice'))
Q4
text = 'code' print(text[2])
Q5
word = 'Python' print(word[5])
Section E: Spot the Error
Q1
C — 'hello' has indices 0-4: index 5 is out of range
Q2
B — First character is at index 0, not 1
Q3
C — Strings are immutable: you can't change individual characters
Q4
C — Index must be an integer, not a string
Q5
B — Use square brackets [] for indexing, not parentheses ()