Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: String Loops

check

Iterating over the characters in a string with string loops.


Loops Through Characters of a String

In Python, for loops can iterate directly over all the characters of a string.

for character in "Hello there!":
  print(character)

> "H"
> "e"
> "l"
> "l"
> "o"
> " "
> "t"
> "h"
> "e"
> "r"
> "e"
> "!"

message = "I am Bob"
compressed_message = ""
for character in message:
  if character.isalpha():
    compressed_message = compressed_message + character.lower()
print(compressed_message)

> "iambob"

There are a couple things to point out in the second example above:

  • Loops and conditionals can be nested inside each other by adding an extra layer of indentation.
  • Another string can be extended by repeatedly concatenating strings and re-assigning the result back to it.

Practice: Count Number of Vowels

  1. Create a string variable named my_string and assign it the value "Hello, World!".

  2. Create a variable vowels and assign it the string of vowels "aeiou".

  3. Create a variable count and initialize it to 0.

  4. Write a for loop to iterate over each character in my_string.

  5. Inside the loop, use an if statement to check if the lower case version of the character is in vowels.

  6. If it is, increment count by 1.

  7. After the loop, print count.

Run your code and confirm that the output is 3 (the number of vowels in "Hello, World!").

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 3
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
my_string = "Hello, World!"
vowels = "aeiou"
count = 0
for character in my_string:
  if character.lower() in vowels:
    count += 1
print(count)

Practice: Reverse a String

  1. Create a string variable named my_string and assign it the value "Python".

  2. Create a new empty string variable named reversed_string.

  3. Write a for loop to iterate over each character in my_string.

  4. Inside the loop, prepend each character to reversed_string.

  5. After the loop, print reversed_string.

Run your code and confirm that the output is "nohtyP".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> "nohtyP"
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
my_string = "Python"
reversed_string = ""
for character in my_string:
  reversed_string = character + reversed_string
print(reversed_string)

Great work so far! Completing these challenges successfully shows your understanding of loops and strings in Python.