Python Program to Find if Character is Vowel or Consonant

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 –

 
A E I O U

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

def isVowel(ch):
    isVowel = False
    if(ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'):
        isVowel = True
    elif(ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U'):
        isVowel = True
    return isVowel

ch = input("Enter a character")
print(isVowel(ch))

Output:

 
Enter a character
a
True

We can improve above code as below –

def isVowel(ch):
    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")
print(isVowel(ch))

Output:

 
Enter a character
o
True

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")

isVowel = False
if(ch.lower() in ('a', 'e', 'i', 'o', 'u')):
    isVowel = True

## Or, if you want to use upper() method.
# if(ch.upper() in ('A', 'E', 'I', 'O', 'U')):
#     isVowel = True

print(isVowel)

Output:

 
Enter a character
a
True

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.

def isVowel(ch):
    chInt = ord(ch)
    isVowel = False
    if(chInt == ord('a') or chInt == ord('e') or chInt == ord('i') or chInt == ord('o') or chInt == ord('u')):
        isVowel = True
    elif(chInt == ord('A') or chInt == ord('E') or chInt == ord('I') or chInt == ord('O') or chInt == ord('U')):
        isVowel = True
    return isVowel

ch = input("Enter a character")
print(isVowel(ch))

Output:

 
Enter a character
A
True

We can improve above code as shown below –

def isVowel(ch):
    chInt = ord(ch)
    isVowel = False
    if(chInt == 97 or chInt == 101 or chInt == 105 or chInt == 111 or chInt == 117):
        isVowel = True
    elif(chInt == 65 or chInt == 69 or chInt == 73 or chInt == 79 or chInt == 85):
        isVowel = True
    return isVowel

ch = input("Enter a character")
print(isVowel(ch))

Output:

Enter a character
I
True

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")
vowels = "aeiouAEIOU"
isVowel = ch in vowels
print(isVowel)

Output:

 
Enter a character
i
True

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')
print(isVowel)

Output:

 
Enter a character
u
True

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']

isVowel = False
for ch in vowels:
    if(ch == chInput):
        isVowel = True
        break

print(isVowel)

Output:

 
Enter a character
U
True

Using Switch Case

Use each vowel letter as a separate case. Returns True, if entered character matches with any switch case.

def isVowel(ch):
    switcher = {
        'A': True,
        'E': True,
        'I': True,
        'O': True,
        'U': True,
        'a': True,
        'e': True,
        'i': True,
        'o': True,
        'u': True
    }
    return switcher.get(ch, False)

ch = input("Enter a character")
print(isVowel(ch))

Output:

 
Enter a character
e
True

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.

def isVowel(ch):
    return (0x208222 >> (0x1f && ord(ch))) & 1

for i in range(26):
    ch = chr(97 + i)
    # 1 if ch is vowel or 0 if consonant.
    print('a is ' + str(isVowel(ch)))

Output:

 
a is 1
b is 0
c is 0
d is 0
e is 1
f is 0
g is 0
h is 0
i is 1
j is 0
k is 0
l is 0
m is 0
n is 0
o is 1
p is 0
q is 0
r is 0
s is 0
t is 0
u is 1
v is 0
w is 0
x is 0
y is 0
z is 0

That’s how can write python program to find if character is vowel or consonant.

Reference: Official Doc

Leave a Reply