In this tutorial, we will learn how to find whether a given character is a vowel or consonant in Python. We will look at five different ways to solve this problem.
Method 1: Using if-else statement
The most straightforward way to solve this problem is to use an if-else statement. We can check if the given character is present in the string ‘aeiouAEIOU’, which contains all the vowels (both upper and lower case). If it is present, we can print that it is a vowel. Otherwise, we can print that it is a consonant.
def is_vowel_or_consonant(ch):
if ch in 'aeiouAEIOU':
print(ch, 'is a vowel')
else:
print(ch, 'is a consonant')
# test the function
is_vowel_or_consonant('a') # a is a vowel
is_vowel_or_consonant('b') # b is a consonant
is_vowel_or_consonant('E') # E is a vowel
is_vowel_or_consonant('f') # f is a consonant
Method 2: Using a list
We can also use a list to store the vowels and check if the given character is present in the list.
def is_vowel_or_consonant(ch):
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
if ch in vowels:
print(ch, 'is a vowel')
else:
print(ch, 'is a consonant')
# test the function
is_vowel_or_consonant('a') # a is a vowel
is_vowel_or_consonant('b') # b is a consonant
is_vowel_or_consonant('E') # E is a vowel
is_vowel_or_consonant('f') # f is a consonant
Method 3: Using a dictionary
We can also use a dictionary to store the vowels and their corresponding values. Then, we can check if the given character is present in the dictionary as a key.
def is_vowel_or_consonant(ch):
vowels = {'a': True, 'e': True, 'i': True, 'o': True, 'u': True,
'A': True, 'E': True, 'I': True, 'O': True, 'U': True}
if ch in vowels:
print(ch, 'is a vowel')
else:
print(ch, 'is a consonant')
# test the function
is_vowel_or_consonant('a') # a is a vowel
is_vowel_or_consonant('b') # b is a consonant
is_vowel_or_consonant('E') # E is a vowel
is_vowel_or_consonant('f') # f is
Method 4: Using a set
Another way to solve this problem is to use a set to store the vowels. Sets are similar to lists and dictionaries, but they do not store values for each element. They are mainly used to check for membership and to remove duplicate elements.
def is_vowel_or_consonant(ch):
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
if ch in vowels:
print(ch, 'is a vowel')
else:
print(ch, 'is a consonant')
# test the function
is_vowel_or_consonant('a') # a is a vowel
is_vowel_or_consonant('b') # b is a consonant
is_vowel_or_consonant('E') # E is a vowel
is_vowel_or_consonant('f') # f is a consonant
Method 5: Using a function from the string module
Python provides a built-in function in the string
module to check if a character is a vowel or consonant. The str.isalpha()
function returns True
if the given character is an alphabet, and False
otherwise. The str.lower()
function converts the character to lowercase, and the str.isvowel()
function returns True
if the character is a vowel, and False
otherwise.
import string
def is_vowel_or_consonant(ch):
if ch.isalpha():
if ch.lower() in 'aeiou':
print(ch, 'is a vowel')
else:
print(ch, 'is a consonant')
else:
print(ch, 'is not an alphabet')
# test the function
is_vowel_or_consonant('a') # a is a vowel
is_vowel_or_consonant('b') # b is a consonant
is_vowel_or_consonant('E') # E is a vowel
is_vowel_or_consonant('f') # f is a consonant
is_vowel_or_consonant('1') # 1 is not an alphabet
Conclusion
In this tutorial, we learned how to find whether a given character is a vowel or consonant in Python. We looked at five different methods to solve this problem using if-else statements, lists, dictionaries, sets, and functions from the string
module. You can choose the method that best suits your needs and use it in your Python programs.