In this article, we will learn about python program to find if character is vowel or consonant.
Getting Started
The task is to check if character entered by user is vowel or consonant. For example,
There are total 26 letters of alphabets –
In Lowercase,
a b c d e f g h i j k l m n o p q r s t u v w x y z |
In Uppercase,
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
Out of these letters, 5 letters are vowels and remaining are consonants.
Vowels are –
Consonants are –
B C D F G H J K L M N P Q R S T V W X Y Z |
We can check if a character is vowel or consonant in many ways –
Using Logical Operator
if (ch = = 'a' or ch = = 'e' or ch = = 'i' or ch = = 'o' or ch = = 'u' ): |
elif (ch = = 'A' or ch = = 'E' or ch = = 'I' or ch = = 'O' or ch = = 'U' ): |
ch = input ( "Enter a character" ) |
Output:
We can improve above code as below –
return (ch = = 'a' or ch = = 'e' or ch = = 'i' or ch = = 'o' or ch = = 'u' ) \ |
or (ch = = 'A' or ch = = 'E' or ch = = 'I' or ch = = 'O' or ch = = 'U' ) |
ch = input ( "Enter a character" ) |
Output:
Using Built-in Method – lower() or upper()
Using lower() or upper() method, we can check if character is vowel or consonant in python as shown below –
ch = input ( "Enter a character" ) |
if (ch.lower() in ( 'a' , 'e' , 'i' , 'o' , 'u' )): |
Output:
Using ASCII Values
ord() function returns ascii value of character. We can use it to compare and find out if ASCII value of entered character is equal to that of vowels or not.
if (chInt = = ord ( 'a' ) or chInt = = ord ( 'e' ) or chInt = = ord ( 'i' ) or chInt = = ord ( 'o' ) or chInt = = ord ( 'u' )): |
elif (chInt = = ord ( 'A' ) or chInt = = ord ( 'E' ) or chInt = = ord ( 'I' ) or chInt = = ord ( 'O' ) or chInt = = ord ( 'U' )): |
ch = input ( "Enter a character" ) |
Output:
We can improve above code as shown below –
if (chInt = = 97 or chInt = = 101 or chInt = = 105 or chInt = = 111 or chInt = = 117 ): |
elif (chInt = = 65 or chInt = = 69 or chInt = = 73 or chInt = = 79 or chInt = = 85 ): |
ch = input ( "Enter a character" ) |
Output:
Using String
Store all vowels in string. Then, check if entered character is found in stored string or not. If found, it is vowel. Otherwise, it’s consonant.
ch = input ( "Enter a character" ) |
Output:
Using Tuple
Store all vowels in Python Tuple. Then, check if entered character is present in tuple or not. If present, it is vowel. Otherwise, it’s consonant.
ch = input ( "Enter a character" ) |
isVowel = ch in ( 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' ) |
Output:
Using List
Store all vowels in python list. Then, run a for loop and check if any character in list matches with entered character or not. If yes, it is vowel.
chInput = input ( "Enter a character" ) |
vowels = [ 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' ] |
Output:
Using Switch Case
Use each vowel letter as a separate case. Returns True, if entered character matches with any switch case.
return switcher.get(ch, False ) |
ch = input ( "Enter a character" ) |
Output:
Using Bit Shift
ASCII Values of Lowercase Vowels in Decimal and Binary are –
Vowel |
Decimal |
Binary |
a |
97 |
01100001 |
e |
101 |
01100101 |
i |
105 |
01101001 |
o |
111 |
01101111 |
u |
117 |
01110101 |
ASCII Values of Uppercase Vowels in Decimal and Binary are –
Vowel |
Decimal |
Binary |
A |
65 |
01000001 |
E |
69 |
01000101 |
I |
73 |
01001001 |
O |
79 |
01001111 |
U |
85 |
01010101 |
Vowels in lowercase or uppercase have same 5 last significant bits. So, we can use character encoding and find a number 2130466 which can gives us 1 in it’s least significant digit. Otherwise, it should gives 0.
Encoding character –
Digits |
Hexadecimal |
Binary |
31 |
0x1F |
00011111 |
2130466 |
0x208222 |
1000001000001000100010 |
How did we found 2130466 ?
Notice it’s digits in binary value. Bit shift this number by 1, 5, 15 and 19 times. It will result in 1 in it’s least significant digit.
return ( 0x208222 >> ( 0x1f && ord (ch))) & 1 |
print ( 'a is ' + str (isVowel(ch))) |
Output:
That’s how can write python program to find if character is vowel or consonant.
Reference: Official Doc