In this post, we write a program to check whether the given character is Vowel or Consonant with the proper explanation but before writing the program we should know what are vowels or consonants?
What are vowels or consonants?
There are 26 characters in the English alphabet and five characters (that is ‘a’, ‘e’, ‘i’, ‘o’, and ‘u‘ ) are known as vowels, and the remaining are known as consonants.
We should write this program in three different ways:
- simple (By taking user input).
- using function.
- except an exception, if the user input wrong data(using try-except).
So let’s write a program for checking vowels or consonants.
1. Simple (By taking user input).
write a program to check vowels or consonants.
Before writing this program you should know about:
Source code:
string = input("enter letter to check vowel or not : ")
string = string.lower()
if len(string)<2:
if string == 'a' or string == 'e' or string == 'i' or string == 'o' or string == 'u':
print(f"{string.upper()} is vowel")
else:
print("not vowel (consonants)")
else:
print("enter only one letter ")
Output-1:
enter letter to check vowel or not : r
not vowel (consonants)
Output-2:
enter letter to check vowel or not : E
E is vowel
Output-3:
enter letter to check vowel or not : RE
enter only one letter
2. Using function
Write a program to check for vowels or consonants using function.
Before writing this program you should know:
Source code:
def check_vowels(string):
new_string = string.lower()
if len(new_string)>1:
return "pls Enter only one charecter."
else:
vowels_list = ['a','e','i','o','u']
if new_string in vowels_list:
return f"{new_string.upper()} is vowel"
else:
return f"{new_string.upper()} is not vowel (consonents)"
charecter = input("enter letter to check vowel or consoents : ")
print(check_vowels(charecter))
Output-1:
enter letter to check vowel or consoents : f
F is not vowel (consonents)
Output-2:
enter letter to check vowel or consoents : U
U is vowel
Output-3:
enter letter to check vowel or consoents : ae
pls Enter only one charecter.
3. except an exception if user input wrong data.
Write a program to check vowels or consonents and if user input wrong data then raise and except a error.
Before writing this program you should know:
Source code:
def check_vowels(string):
new_string = string.lower()
vowels_list = ['a','e','i','o','u']
if new_string in vowels_list:
return f"{new_string.upper()} is vowel"
else:
return f"{new_string.upper()} is not vowel (consonents)"
while True:
char = input("Enter letters to check vowels or consonents : ")
try:
if len(char)>1:
raise TypeError
else:
break
except TypeError as t:
print("pls enter single charecter only.")
print(check_vowels(char))
Output-1:
Enter letters to check vowels or consonents : aa
pls enter single charecter only.
Enter letters to check vowels or consonents : a
A is vowel
Output-2:
Enter letters to check vowels or consonents : B
B is not vowel (consonents)
More python programs for your practice:
- python program to check perfect number.
- python program to check for prime number.
- Create simple hangman game in python.
Hope this post adds some value to your life – thankyou.