Recall & Review
beginner
What does it mean to iterate over a string in Python?
Iterating over a string means going through each character one by one, like reading a word letter by letter.
Click to reveal answer
beginner
How do you write a simple loop to print each character of the string
"hello"?Use a
for loop like this:<br>for char in "hello":
print(char)Click to reveal answer
beginner
Can you change characters of a string by iterating over it?
No, strings are like frozen words. You can read each letter, but you cannot change them directly because strings are immutable.
Click to reveal answer
beginner
What will this code print?<br>
for c in "abc":
print(c.upper())It prints:<br>
A B C<br>Each letter is converted to uppercase before printing.
Click to reveal answer
intermediate
How can you get the index of each character while iterating over a string?
Use the
enumerate() function:<br>for index, char in enumerate("hello"):
print(index, char)Click to reveal answer
What does the following code do?<br>
for ch in "cat":
print(ch)✗ Incorrect
The loop goes through each character and prints it, so each letter appears on its own line.
Which statement is true about strings in Python?
✗ Incorrect
Strings cannot be changed after creation; they are immutable.
How do you get both the index and character when looping over a string?
✗ Incorrect
The enumerate() function gives both index and character during iteration.
What will this code print?<br>
for c in "dog":
print(c.upper())✗ Incorrect
Each character is printed in uppercase on its own line.
Which of these is NOT a way to iterate over a string?
✗ Incorrect
Strings do not have a reverse() method; you can reverse with slicing but not by reverse() method.
Explain how to loop through each character in a string and print it.
Think about reading a word letter by letter.
You got /3 concepts.
Describe why you cannot change characters in a string while iterating over it.
Strings are like frozen words.
You got /3 concepts.