How to Check if a String contains Vowels in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
7 min

banner

# Table of Contents

  1. Check if a String contains Vowels in Python
  2. Getting the vowels that are contained in the string
  3. Counting the vowels in the string
  4. Check if a letter is a Vowel in Python
  5. Checking if each letter in a string is a vowel or a consonant
  6. Find words starting with a Vowel in a List in Python

# 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

check if string contains vowels

The code for this article is available on GitHub

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

This is a three-step process:

  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

check if string contains vowels using for loop

The code for this article is available on GitHub

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.

# Getting the vowels that are contained in the string

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']

getting vowels that are contained in string

The code for this article is available on GitHub

We used the not in operator to make sure we weren'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']

# Counting the vowels in the string

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
The code for this article is available on GitHub

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.

# Check if a letter is a Vowel in Python

Use the in operator to check if a letter is a vowel.

The in operator will return True if the letter is a vowel and False otherwise.

main.py
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] my_str = 'bobbyhadz' if my_str[0] in vowels: print('The letter is a vowel') else: # ๐Ÿ‘‡๏ธ this runs print('The letter is a consonant') my_str = 'abcde' print(my_str[0] in vowels) # ๐Ÿ‘‰๏ธ True print(my_str[1] in vowels) # ๐Ÿ‘‰๏ธ False print(my_str[-1] in vowels) # ๐Ÿ‘‰๏ธ True
The code for this article is available on GitHub

The vowels variable stores a list containing the lowercase and uppercase vowels.

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

The following example checks if the first letter of the string is a vowel.

main.py
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] my_str = 'bobbyhadz' if my_str[0] in vowels: print('The letter is a vowel') else: # ๐Ÿ‘‡๏ธ this runs print('The letter is a consonant')

If the letter is not a vowel, then it is a consonant.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.
main.py
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] my_str = 'abcde' print(my_str[0] in vowels) # ๐Ÿ‘‰๏ธ True print(my_str[1] in vowels) # ๐Ÿ‘‰๏ธ False print(my_str[-1] in vowels) # ๐Ÿ‘‰๏ธ True

Negative indices can be used to count backward, e.g. my_str[-1] returns the last character in the string and my_str[-2] returns the second-to-last character.

# Checking if each letter in a string is a vowel or a consonant

You can use a for loop to check if each letter in a string is a vowel or a consonant.

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']
The code for this article is available on GitHub

We used the list.append() method to append the vowels from the string to a new list.

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']

The method returns None as it mutates the original list.

# Find words starting with a Vowel in a List in Python

To find the words that start with a vowel in a list:

  1. Use a for loop to iterate over the list.
  2. Check if the first character of each word is a vowel.
  3. Append the words that start with a vowel to a new list.
main.py
my_list = ['one', 'two', 'age', 'hello', 'example'] vowels = 'aeiouAEIOU' starting_with_vowel = [] for word in my_list: if word[0] in vowels: starting_with_vowel.append(word) print(starting_with_vowel) # ๐Ÿ‘‰๏ธ ['one', 'age', 'example']

We used a for loop to iterate over the list.

On each iteration, we access the first character in the string and check if it is a vowel.

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

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.

main.py
vowels = 'aeiouAEIOU' print('avocado'[0] in vowels) # ๐Ÿ‘‰๏ธ True print('bobbyhadz'[0] in vowels) # ๐Ÿ‘‰๏ธ False

If the word starts with a vowel, we append it to a new list.

main.py
my_list = ['one', 'two', 'age', 'hello', 'example'] vowels = 'aeiouAEIOU' starting_with_vowel = [] for word in my_list: if word[0] in vowels: starting_with_vowel.append(word) print(starting_with_vowel) # ๐Ÿ‘‰๏ธ ['one', 'age', 'example']

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

Alternatively, you can use a list comprehension.

# Find words starting with a Vowel in a List in Python

This is a three-step process:

  1. Use a list comprehension to iterate over the list.
  2. Check if the first character of each word is a vowel.
  3. The new list will only contain the words that start with a vowel.
main.py
my_list = ['one', 'two', 'age', 'hello', 'example'] vowels = 'aeiouAEIOU' starting_with_vowel = [word for word in my_list if word[0] in vowels] print(starting_with_vowel) # ๐Ÿ‘‰๏ธ ['one', 'age', 'example']
The code for this article is available on GitHub

We used a list comprehension to iterate over the list.

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

On each iteration, we check if the word starts with a vowel and return the result.

The new list only contains the words that start with a vowel.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev