Last updated: Apr 9, 2024
Reading timeยท7 min

To check if a string contains vowels:
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.
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.
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.
This is a three-step process:
for loop to iterate over the string.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).
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 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.
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.
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.
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.
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 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.
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.
0, and the last character has an index of -1 or len(my_str) - 1.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.
You can use a for loop to check if each letter in a string is a vowel or a
consonant.
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 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.
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The method returns None as it mutates the original list.
To find the words that start with a vowel in a list:
for loop to iterate over the list.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.
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.
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.
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.
This is a three-step process:
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']
We used a list comprehension to iterate over the list.
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.
You can learn more about the related topics by checking out the following tutorials: