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: for char in s: print(char) This prints each character one by one.
Click to reveal answer
What is the index of the first character in a string?
ADepends on the string length
B1
C-1
D0
✗ Incorrect
In most programming languages including Python, string indexing starts at 0.
What will print('hello'[1]) output?
Al
Bh
Ce
Do
✗ Incorrect
Index 1 is the second character, which is 'e' in 'hello'.
Which of these will cause an error?
AAccessing <code>string[0]</code> in a non-empty string
BAccessing <code>string[len(string)]</code>
CLooping through string with <code>for char in string</code>
DUsing <code>string[-1]</code> to get last character
✗ 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?
A<code>s[-1]</code>
B<code>s[len(s)]</code>
C<code>s[0]</code>
D<code>s[len(s)-2]</code>
✗ Incorrect
Negative index -1 accesses the last character in Python strings.
What does this code do? for i in range(len(s)): print(s[i])
APrints each character in string s one by one
BPrints the length of s
CPrints the string s reversed
DCauses an error
✗ 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.
Practice
(1/5)
1. What does the expression my_string[2] return when my_string = 'hello'?
easy
A. An error because index 2 is out of range
B. The first character 'h'
C. The third character 'l'
D. The last character 'o'
Solution
Step 1: Understand string indexing
Strings in Python start indexing at 0, so index 2 means the third character.
Step 2: Identify the character at index 2
In 'hello', index 0='h', index 1='e', index 2='l'.
Final Answer:
The third character 'l' -> Option C
Quick Check:
Index 2 of 'hello' is 'l' [OK]
Hint: Remember string indexing starts at zero [OK]
Common Mistakes:
Thinking indexing starts at 1
Confusing index 2 with last character
Assuming index 2 is out of range
2. Which of the following is the correct way to loop through each character in the string text = 'abc'?
easy
A. for char in text: print(char)
B. for char in text[1]: print(char)
C. for i in range(text): print(i)
D. for i in text.length: print(text[i])
Solution
Step 1: Understand how to loop over a string
Using for char in text iterates over each character directly.
Step 2: Check each option
for i in range(text): print(i) tries to use range on a string (invalid). for char in text[1]: print(char) uses text[1] which is a single character, not iterable. for i in text.length: print(text[i]) uses text.length which is invalid in Python.
Final Answer:
for char in text: print(char) -> Option A
Quick Check:
Loop over string directly with for-in [OK]
Hint: Use 'for char in string' to loop characters [OK]
Common Mistakes:
Using range() directly on string
Using incorrect string length property
Trying to loop over a single character
3. What is the output of the following code?
word = 'python'
result = ''
for i in range(len(word)):
if i % 2 == 0:
result += word[i]
print(result)
medium
A. pyo
B. pto
C. pyhn
D. pthn
Solution
Step 1: Understand the loop and condition
The loop runs from i=0 to i=5 (length 6). It adds characters at even indices (0,2,4).