Check if a String contains Vowels in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Check if a String contains Vowels in Python #

To check if a string contains vowels:

  1. Use a generator expression to iterate over the string.
  2. Check if each character is a vowel.
  3. If the condition is met for any of the characters, the string contains vowels.
main.py
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} my_str = 'bobby' if any(char in vowels for char in my_str): # 👇️ this runs print('The string contains at least one vowel') else: print('The string does NOT contain any vowels') print(any(char in vowels for char in 'hadz')) # 👉️ True print(any(char in vowels for char in 'hdz')) # 👉️ False print(any(char in vowels for char in '')) # 👉️ False

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the in operator to check if the current character is contained in the vowels set.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

main.py
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} print(any(char in vowels for char in 'hadz')) # 👉️ True print(any(char in vowels for char in 'hdz')) # 👉️ False print(any(char in vowels for char in '')) # 👉️ False

If the condition is met for any of the characters in the string, the any() function returns True and short-circuits.

Alternatively, you can use a for loop.

Check if a String contains Vowels using a for loop #

To check if a string contains vowels:

  1. Use a for loop to iterate over the string.
  2. Check if each character is a vowel.
  3. If the condition is met, the string contains vowels.
main.py
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} def contains_vowels(string): for char in string: if char in vowels: return True return False print(contains_vowels('bobby')) # 👉️ True print(contains_vowels('hadz')) # 👉️ True print(contains_vowels('hdz')) # 👉️ False print(contains_vowels('')) # 👉️ False

We used a for loop to iterate over the string.

On each iteration, we check if the current character is a vowel.

If the condition is met, we return True to exit out of the function.

If the condition is never met, the function returns False.

You can use the same approach to get the vowels that are contained in the string (if any).
main.py
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} my_str = 'abcde' vowels_in_string = [] for char in my_str: if char in vowels: print(f'{char} is a vowel') if char not in vowels_in_string: vowels_in_string.append(char) else: print(f'{char} is a consonant') # a is a vowel # b is a consonant # c is a consonant # d is a consonant # e is a vowel print(vowels_in_string) # 👉️ ['a', 'e']

We used the not in operator to make sure we aren't adding duplicate vowels to the vowels_in_string list.

x not in l returns the negation of x in l.

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', 'com']

If you need to count the vowels in the string, create a dictionary with vowels as keys.

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

We used a dict comprehension to iterate over a string containing the vowels.

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.

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

The vowels_count dictionary contains the number of occurrences of each vowel 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.