Open In App

Python Program to find if a character is vowel or Consonant

Last Updated : 17 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Given a character, check if it is vowel or consonant. Vowels are 'a', 'e', 'i', 'o' and 'u'. All other characters ('b', 'c', 'd', 'f' ....) are consonants.

Examples:  

Input : x = 'c'
Output : Consonant
Input : x = 'u'
Output : Vowel

We check whether the given character matches any of the 5 vowels. If yes, we print "Vowel", else we print "Consonant". 

Python3
# Python3 program to check if a given 
# character is vowel or consonant.

# Function to check whether a character 
# is vowel or not
def vowelOrConsonant(x):

    if (x == 'a' or x == 'e' or
        x == 'i' or x == 'o' or x == 'u'):
        print("Vowel")
    else:
        print("Consonant")

# Driver code
vowelOrConsonant('c')
vowelOrConsonant('e')
   

Output
Consonant
Vowel

Time Complexity: O(1)
Auxiliary Space: O(1)

How to handle capital letters as well?  

Python3
# Python3 program to check if a given 
# character is vowel or consonant.

# Function to check whether a 
# character is vowel or not
def vowelOrConsonant(x):
    if (x == 'a' or x == 'e' or x == 'i' or 
        x == 'o' or x == 'u' or x == 'A' or 
        x == 'E' or x == 'I' or x == 'O' or 
        x == 'U'):
        print("Vowel")
    else:
        print("Consonant")

# Driver code
if __name__ == '__main__':
    vowelOrConsonant('c')
    vowelOrConsonant('E')

Output
Consonant
Vowel

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Program to find if a character is vowel or Consonant using switch case

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

# Driver Code
print('a is '+isVowel('a'))
print('x is '+isVowel('x'))

Output
a is Vowel
x is Consonant

Time Complexity: O(1)
Auxiliary Space: O(1)

Another way is to find() the character in a string containing only Vowels.

Python3
def isVowel(ch):

    # Make the list of vowels
    str = "aeiouAEIOU"
    return (str.find(ch) != -1)

# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))

Output
a is True
x is False

Time Complexity: O(N)
Auxiliary Space: O(1)

Most efficient way to check Vowel using bit shift :

In ASCII these are the respective values of every vowel both in lower and upper cases.

VowelDECHEXBINARY

a

970x6101100001

e

1010x6501100101

i

1050x6901101001

o

1110x6F01101111

u

1170x7501110101

 

A

650x4101000001

E

690x4501000101

I

730x4901001001

O

790x4F01001111

U

850x5501010101


 

As very lower and upper case vowels have the same 5 LSBs. We need a number 0x208222 which gives 1 in its LSB after right-shift 1, 5, 19, 15 otherwise gives 0.  The numbers depend on character encoding.


 

DECHEXBINARY
310x1F00011111
21304660x2082221000001000001000100010


 

Python3
def isVowel(ch):

    return (0x208222 >> (ord(ch) & 0x1f)) & 1
    # same as (2130466 >> (ord(ch) & 31)) & 1;

# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))

Output
a is 1
x is 0

Time Complexity: O(1)
Auxiliary Space: O(1)

*We can omit the ( ch & 0x1f ) part on X86 machines as the result of SHR/SAR (which is >> ) masked to 0x1f automatically.
*For machines bitmap check is faster than table check, but if the ch variable stored in register than it may perform faster.

Use regular expressions:

Using a regular expression to check if a character is a vowel or consonant. Regular expressions are a powerful tool for pattern matching and can be used to quickly and easily check if a given character matches a specific pattern.

To check if a character is a vowel using a regular expression, you can use the following code:

Python3
import re

def is_vowel(char):
    if re.match(r'[aeiouAEIOU]', char):
        return True
    return False

print(is_vowel('a'))  # Output: True
print(is_vowel('b'))  # Output: False

Output
True
False

Time complexity: O(1), as the re.match function has a time complexity of O(1) for small strings. In this case, the string char is a single character and therefore has a small constant size.
Auxiliary space: O(1), as we are only using a constant amount of memory for the char argument and the return value.

The regular expression r'[aeiouAEIOU]' matches any character that is a lowercase or uppercase vowel. The re.match() function searches for a match at the beginning of the string, so it will return True if the given character is a vowel and False otherwise.

This approach has the advantage of being concise and easy to understand, and it can be easily modified to check for other patterns as well. However, it may not be as efficient as some of the other approaches mentioned in the article, particularly for large inputs.

Please refer complete article on Program to find if a character is vowel or Consonant for more details!

Using ord() method:

Python3
la=[97,101,105,111,117]
ua=[65,69,73,79,85]
def isVowel(ch):
    if ord(ch) in la or ord(ch) in ua:
        return True
    return False
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))

Output
a is True
x is False

Time Complexity: O(1)
Auxiliary Space: O(1)

Using operator.countOf() method

Python3
import operator as op

vowels="aeiouAEIOU"

def is_vowel(char):
    if op.countOf(vowels, char)>0:
        return True
    return False

print(is_vowel('a'))  # Output: True
print(is_vowel('b'))  # Output: False

Output
True
False

Time Complexity: O(n)
Auxiliary Space: O(1)


Practice Tags :

Similar Reads