Recall & Review
beginner
What does string traversal mean?
String traversal means going through each character in a string one by one, usually from the first character to the last.
Click to reveal answer
beginner
How do you access the third character in a string named
text in Python?You use
text[2] because counting starts at zero. So the first character is text[0], second is text[1], and third is text[2].Click to reveal answer
beginner
What happens if you try to access a character at an index that is outside the string length?
You get an error called
IndexError because the position does not exist in the string.Click to reveal answer
intermediate
Why is zero-based indexing used in string traversal?
Zero-based indexing means counting starts at 0. It helps computers calculate positions easily and is a common standard in programming.
Click to reveal answer
beginner
How can you loop through each character in a string
s in Python?You can use a
for loop like this:<br>for char in s:<br> print(char)<br>This prints each character one by one.Click to reveal answer
What is the index of the first character in a string?
✗ Incorrect
In most programming languages including Python, string indexing starts at 0.
What will
print('hello'[1]) output?✗ Incorrect
Index 1 is the second character, which is 'e' in 'hello'.
Which of these will cause an error?
✗ Incorrect
Index equal to string length is out of range and causes IndexError.
How do you get the last character of a string
s in Python?✗ Incorrect
Negative index -1 accesses the last character in Python strings.
What does this code do?<br>
for i in range(len(s)):<br> print(s[i])✗ Incorrect
It loops over each index and prints the character at that index.
Explain how to access characters in a string and what happens if you use an invalid index.
Think about how counting starts and what happens if you go beyond the string length.
You got /3 concepts.
Describe two ways to traverse a string in Python and print each character.
One way uses characters directly, the other uses indexes.
You got /2 concepts.