📖 Scenario: Imagine you are creating a simple program that reads a word and checks each letter one by one. This is like reading a word slowly to understand each letter.
🎯 Goal: You will build a program that stores a word, sets up a counter, goes through each letter in the word, and prints each letter on its own line.
📋 What You'll Learn
Create a string variable with the exact value 'hello'
Create an integer variable called index and set it to 0
Use a while loop with the condition index < len(word)
Inside the loop, print the character at position index in word
Increase index by 1 inside the loop
Print each character on its own line
💡 Why This Matters
🌍 Real World
Reading and processing text one character at a time is useful in text editors, spell checkers, and simple parsers.
💼 Career
Understanding string traversal is a basic skill needed for programming tasks like data cleaning, input validation, and building text-based applications.
Progress0 / 4 steps
1
Create the word variable
Create a string variable called word and set it to the exact value 'hello'.
DSA Python
Hint
Use single quotes around the word hello.
2
Set up the index counter
Create an integer variable called index and set it to 0.
DSA Python
Hint
This variable will help us move through each letter in the word.
3
Traverse the word using a while loop
Use a while loop with the condition index < len(word). Inside the loop, print the character at position index in word. Then increase index by 1.
DSA Python
Hint
Remember to increase the index inside the loop to avoid an infinite loop.
4
Print each character on its own line
Run the program to print each character of word on its own line using the print statement inside the loop.
DSA Python
Hint
The output should show each letter of 'hello' on a new line.
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).