Challenge - 5 Problems
String Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of iterating and modifying a string
What is the output of this Python code?
text = "hello"
result = ""
for ch in text:
if ch == 'l':
result += 'L'
else:
result += ch
print(result)Python
text = "hello" result = "" for ch in text: if ch == 'l': result += 'L' else: result += ch print(result)
Attempts:
2 left
💡 Hint
Remember that the loop checks each character and replaces 'l' with 'L'.
✗ Incorrect
The loop goes through each character in 'hello'. When it finds 'l', it adds 'L' to result. Otherwise, it adds the original character. So 'l' becomes 'L', resulting in 'heLLo'.
❓ Predict Output
intermediate2:00remaining
Counting vowels in a string
What is the output of this code?
text = "Programming"
vowels = 'aeiouAEIOU'
count = 0
for char in text:
if char in vowels:
count += 1
print(count)Python
text = "Programming" vowels = 'aeiouAEIOU' count = 0 for char in text: if char in vowels: count += 1 print(count)
Attempts:
2 left
💡 Hint
Check each letter if it is a vowel.
✗ Incorrect
The vowels in 'Programming' are 'o', 'a', and 'i', so the count is 3.
❓ Predict Output
advanced2:00remaining
Output of nested iteration over string
What does this code print?
word = "abc"
result = ""
for i in word:
for j in word:
if i == j:
result += i.upper()
else:
result += j
print(result)Python
word = "abc" result = "" for i in word: for j in word: if i == j: result += i.upper() else: result += j print(result)
Attempts:
2 left
💡 Hint
Look at how the inner loop builds the string for each outer loop character.
✗ Incorrect
For each character i in 'abc', the inner loop goes through 'abc'. When i equals j, it adds uppercase i, else adds j. So for i='a', it adds 'A', 'b', 'c' → 'Abc'. For i='b', adds 'a', 'B', 'c' → 'aBc'. For i='c', adds 'a', 'b', 'C' → 'abC'. Combined: 'AbcaBcabC'. Option B matches this.
❓ Predict Output
advanced2:00remaining
Result of modifying string characters conditionally
What is the output of this code?
text = "Data123"
result = ""
for ch in text:
if ch.isalpha():
result += ch.lower()
else:
result += '*'
print(result)Python
text = "Data123" result = "" for ch in text: if ch.isalpha(): result += ch.lower() else: result += '*' print(result)
Attempts:
2 left
💡 Hint
Check how letters and digits are handled differently.
✗ Incorrect
Letters are converted to lowercase, digits replaced by '*'. So 'Data123' becomes 'data***'.
🧠 Conceptual
expert2:00remaining
Why strings are immutable in Python?
Which of the following is the main reason strings are immutable in Python?
Attempts:
2 left
💡 Hint
Think about what properties dictionary keys need.
✗ Incorrect
Strings are immutable so they can be hashed and used safely as dictionary keys and set elements without risk of changing their value.