Count the Vowels in a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Count the Vowels in a String in Python #

To count the vowels in a string:

  1. Store the vowels in a string.
  2. Use a dict comprehension to iterate over the string.
  3. Use the str.count() method to count the occurrences of each vowel in the original string.
main.py
vowels = 'aeiou' my_str = 'bobbyhadz.com' # ✅ count the number of occurrences of each vowel in the string vowels_count = {vowel: my_str.lower().count(vowel) for vowel in vowels} print(vowels_count) # 👉️ {'a': 1, 'e': 0, 'i': 0, 'o': 2, 'u': 0} print(vowels_count['a']) # 👉️ 1 print(vowels_count['o']) # 👉️ 2 # ------------------------------------------------------- # ✅ count how many vowels there are in the string vowels_count = 0 for char in my_str: if char.lower() in vowels: vowels_count += 1 print(vowels_count) # 👉️ 3

The first example uses a dict comprehension to count the number of occurrences of each vowel in the string.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.
main.py
vowels = 'aeiou' my_str = 'bobbyhadz.com' vowels_count = {vowel: my_str.lower().count(vowel) for vowel in vowels} print(vowels_count) # 👉️ {'a': 1, 'e': 0, 'i': 0, 'o': 2, 'u': 0} print(vowels_count['a']) # 👉️ 1 print(vowels_count['o']) # 👉️ 2

On each iteration, we use the str.count() method to count the number of occurrences of each vowel in the string.

The str.count method returns the number of occurrences of a substring in a string.

main.py
print('aabc'.count('a')) # 👉️ 2 print('aabc'.count('x')) # 👉️ 0

If you need to distinguish between lowercase and uppercase vowels, remove the call to the str.lower() method.

main.py
# 👇️ added capital vowels vowels = 'aeiouAEIOU' my_str = 'bobbyhadz.com' vowels_count = {vowel: my_str.count(vowel) for vowel in vowels} # 👇️ {'a': 1, 'e': 0, 'i': 0, 'o': 2, 'u': 0, 'A': 0, 'E': 0, 'I': 0, 'O': 0, 'U': 0} print(vowels_count) print(vowels_count['a']) # 👉️ 1 print(vowels_count['o']) # 👉️ 2

Alternatively, you can use a for loop.

Count the Vowels in a String using a for loop #

To count the vowels in a string:

  1. Use a for loop to iterate over a string containing the vowels.
  2. Use the str.count() method to count the occurrences of each vowel in the original string.
  3. Assign each vowel and its number of occurrences to a dictionary.
main.py
vowels = 'aeiou' my_str = 'bobbyhadz.com' vowels_count = {} for vowel in vowels: vowels_count[vowel] = my_str.lower().count(vowel) print(vowels_count) # 👉️ {'a': 1, 'e': 0, 'i': 0, 'o': 2, 'u': 0}

This approach is very similar to the last one, but instead of using a dict comprehension, we used a for loop.

On each iteration, we add a key-value pair to the dictionary with the vowel as the key and its number of occurrences in the string as the value.

If you just need to get the total number of vowels there are in the string, initialize a count variable to 0 and increment it for each vowel.

main.py
vowels = 'aeiou' my_str = 'bobbyhadz.com' vowels_count = 0 for char in my_str: if char.lower() in vowels: vowels_count += 1 print(vowels_count) # 👉️ 3

We used a for loop to iterate over the string.

On each iteration, we convert the current character to lowercase and check if it's a vowel.

If the condition is met, we increment the vowels_count variable.

After the last iteration, the variable stores the total number of vowels in the string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.