How to Check for Vowels in Python Strings and Lists
This guide explains how to check if a string contains vowels, how to identify vowels within a string, and how to find words starting with vowels in a list. We'll use efficient and Pythonic techniques, including the in
operator, list comprehensions, and generator expressions.
Checking if a String Contains Any Vowels
The most Pythonic way to check if a string contains at least one vowel is to use the any()
function with a generator expression:
Using any()
and a Generator Expression (Recommended)
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} # Use a set for efficiency
my_str = 'tutorialreference'
if any(char in vowels for char in my_str): # Generator expression
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 'reference')) # Output: True
print(any(char in vowels for char in 'rfnc')) # Output: False
print(any(char in vowels for char in '')) # Output: False
- A set containing all vowels is created for efficiency.
any(char in vowels for char in my_str)
: This is a generator expression. It efficiently checks each character (char
) inmy_str
to see if it's present in thevowels
set.any()
returnsTrue
as soon as it finds a vowel (short-circuiting), andFalse
if it reaches the end of the string without finding a vowel.- Using
set
forvowels
: Using a set ({'a', 'e', ...}
) forvowels
is significantly more efficient than using a list or string for thein
check. Set lookups are O(1) (constant time) on average, while list/string lookups are O(n) (linear time).
Using a for
Loop
You can use a for
loop, but it's less concise and less efficient:
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 immediately upon finding a vowel
return False # No vowels found after checking all characters
- Using the for loop to find the vowel is more verbose and less efficient than using
any()
.
Identifying and Extracting Vowels from a String
To get a list of the vowels present in a string (without duplicates):
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: # Check for repetition
vowels_in_string.append(char)
else:
print(f'{char} is a consonant')
# Output:
# a is a vowel
# b is a consonant
# c is a consonant
# d is a consonant
# e is a vowel
print(vowels_in_string) # Output: ['a', 'e']
- This uses a
for
loop to iterate over all the characters, and appends the vowels to a new list, only if not already appended.
Counting Vowels in a String
To count the number of vowels in a string:
vowels = 'aeiou' # Only lowercase for simplicity; add 'AEIOU' if needed.
my_str = 'tutorialreference.com'
vowels_count = {vowel: my_str.lower().count(vowel) for vowel in vowels}
print(vowels_count) # Output: {'a': 1, 'e': 4, 'i': 1, 'o': 2, 'u': 1}
- This uses a dictionary comprehension to create a dictionary that stores the counts of each vowel in the string.
Checking if a Single Character is a Vowel
To check if a single character is a vowel, the in
operator is the most direct and readable way:
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] # Or use a set
my_str = 'tutorialreference'
if my_str[0] in vowels: # Check if the character is a vowel
print('The letter is a vowel')
else:
print('The letter is a consonant') # Output: The letter is a consonant
- The
in
operator is used to check if a character is present in a string or a list.
Finding Words Starting with Vowels in a List
To find words in a list that begin with a vowel:
Using a List Comprehension
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) # Output: ['one', 'age', 'example']
Using a for
Loop
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) # Append to the list if starts with vowel.
print(starting_with_vowel) # Output: ['one', 'age', 'example']