Flow control

Looping through a String

Looping through a string in Python means iterating over each character in the string one by one, using a loop. Since a string is a sequence of characters, you can treat it like any other iterable (such as a list or tuple) in a for loop.

1. Using a for loop to iterate over a String:

word = "Python"
for letter in word:
    print(letter)

  • The string Python is assigned to the variable word.
  • The for loop iterates over the string character by character.
  • On each iteration, the variable letter takes the value of the current character from the string.
  • The print(letter) statement outputs the character for each iteration.

Output:

P
y
t
h
o
n

 
2. Using a while loop to iterate over a String:

You can also loop through a string using a while loop by manually tracking the index of each character.

Example:

word = "Python"
i = 0
while i < len(word):
    print(word[i])
    i += 1

  • The variable i is initialized to 0, which will be used as the index.
  • The while loop continues as long as i is less than the length of the string (len(word)).
  • The string's character at index i is printed with word[i].
  • After each iteration, i is incremented by 1 to move to the next character.

Output:

P
y
t
h
o
n


3. Looping through a string with range():

You can use range() in combination with the length of the string to iterate through its characters by their index.

Example:

word = "Python"
for i in range(len(word)):
    print(word[I])

  • range(len(word)) generates a range of numbers from 0 to the length of the string minus one.
  • On each iteration, i takes the value of the current index, and word[i] accesses the character at that index.

Output:

P
y
t
h
o
n


4. Use cases:

Checking characters in a string: While looping through a string, you can use conditions to check or manipulate characters.

Example:

sentence = "Hello World"
for char in sentence:
    if char == 'o':
        print("Found 'o'")
    else:
        print(char)

  • The loop checks each character in the string sentence.
  • If the character is o, it prints Found o.
  • Otherwise, it prints the character itself.

Output:

H
e
l
l
Found 'o'
 
W
Found 'o'
r
l
d


Here’s another simple example of counting vowels in a string:

word = "Programming"
vowels = "aeiouAEIOU"
count = 0

for letter in word:
    if letter in vowels:
        count += 1

print("Number of vowels:", count)

Output:

Number of vowels: 3

This code loops through each letter of the word Programming, checks if it's a vowel, and increments the count variable accordingly.

Looping through a string is useful when you need to inspect or manipulate individual characters.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What will be the output of the following code? word = "Python" for letter in word: print(letter)

P y t h o n
P
P y t h o n
Python
Check Answer

What will be the output of the following code? word = "Python" i = 0 while i < len(word): print(word[i]) i += 1

P y t h o n
Python
P y t h o n
P
Check Answer

What is the output of the following code? word = "Python" for i in range(len(word)): print(word[i])

P y t h o n
P
P y t h o n
Python
Check Answer

What will be the output of the following code? sentence = "Hello World" for char in sentence: if char == 'o': print("Found 'o'") else: print(char)

H e l l Found 'o' W Found 'o' r l d
Hello World
H e l l o W o r l d
Found 'o'
Check Answer

What will be the output of the following code? word = "Programming" vowels = "aeiouAEIOU" count = 0 for letter in word: if letter in vowels: count += 1 print("Number of vowels:", count)

Number of vowels: 4
Number of vowels: 1
Number of vowels: 2
Number of vowels: 3
Check Answer