Hello everyone, welcome to nkcoderz.com. In this article we will going to discuss about Python program to check whether a given alphabet is a vowel or consonant.
Here is an example of a Python program that can be used to check whether a given alphabet is a vowel or a consonant:
Table of Contents
Code for python program to check whether a given alphabet is a vowel or consonant
def check_vowel_consonant(alphabet):
vowels = 'aeiou'
if alphabet.lower() in vowels:
return 'Vowel'
else:
return 'Consonant'
alphabet = input("Enter a letter of the alphabet: ")
print(check_vowel_consonant(alphabet))
Output
Enter a letter of the alphabet:
aVowel
Explanation
This program defines a function called check_vowel_consonant()
that takes a single argument, alphabet
, which should be a single letter of the alphabet. The function first defines a string called vowels
that contains the five vowels of the alphabet. Then, it checks whether the lowercase version of alphabet
is contained in the vowels
string. If it is, the function returns the string “Vowel”; otherwise, it returns the string “Consonant”.
When you run the program, it will prompt you to enter a letter of the alphabet, and then it will print either “Vowel” or “Consonant” depending on whether the input letter is a vowel or a consonant.
You could also use isalpha() method provided by python which check for letter, isalpha() return true if the letter is alphabet otherwise it returns false, so we can check with the help of that.
def check_vowel_consonant(alphabet):
vowels = 'aeiou'
if alphabet.isalpha() and len(alphabet) == 1:
if alphabet.lower() in vowels:
return 'Vowel'
else:
return 'Consonant'
else:
return "Invalid Input"
alphabet = input("Enter a letter of the alphabet: ")
print(check_vowel_consonant(alphabet))
This program will check the input whether it is letter and also single letter, if not it will return “Invalid Input”
Python program to check the given character is alphabet or digit or special character
Conclusion
If you liked this post, then please share this with your friends and make sure to bookmark this website for more awesome content.