Python Program for checking a character is a vowel or consonant

Checking a Character is a vowel or consonant in Python

Here, in this section we will discuss the program to check the entered character is a vowel or consonant in python. In Python string is an array representation of Characters python does not have a character data type. A single character is a string of length [1].
Vowels:- A character is considered as a vowel when it belongs to the set of characters like { ‘A’ , ‘E’ , ‘I’ , ‘O’ , ‘U’ }

Character is a vowel or consonant in C

Working:-

  • Take character input from the user
  • Check if Input is a lowercase of upper case vowel
  • If yes then print vowel
  • If not then print consonant
  • Can also additional check if it’s a non-character item

We will discuss various methods to do the same thing.

C++ program to check whether Character is a vowel or consonant

Method 1

Now in here we will see how we can identify whether a character is a vowel or consonant using the Java programming language.

Python Code

Run
c = 'a'

# checking for vowels
if c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u' or c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U':
    print(c, "is a vowel")  # condition true input is vowel
else:
    print(c, "is a consonant")  # condition true input is consonant

Output

a is a vowel

Method 2

The issue with the previous method was that we were not checking if the user entered a non-alphabet character like ‘3’ or ‘%’ etc. We will see an alternate approach and also handle this non-alphabet case.

Python Code

Run
def isLowercaseVowel(c):
    # returns 1 if char matches any of below
    return c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u'


def isUppercaseVowel(c):
    # returns 1 if char matches any of below
    return c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U'


c ='f'

# show error message if c is not an alphabet
if not c.isalpha():
    print("Non alphabet")
elif isLowercaseVowel(c) or isUppercaseVowel(c):
    print(c, "is a Vowel")
else:
    print(c, "is a consonant")

Output

f is a consonant

Method 3

The above method has two separate functions of upper case vowels and lowercase vowels we can reduce that down to one single method using an inbuilt function that converts any lowercase case charter to uppercase.

Python Code

Run
# single function for both uppercase and lowercase
def isVowel(c):
    # converts to uppercase if it wasn't already
    c=c.upper()
    # returns true if char matches any of below
    return c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U'


c = 'f'

# show error message if c is not an alphabet
if not c.isalpha():
    print("Non alphabet")
elif isVowel(c):
    print(c, "is a Vowel")
else:
    print(c, "is a consonant")

Output

f is a consonant

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

For similar questions click on given button

7 comments on “Python Program for checking a character is a vowel or consonant”


  • Sangram

    print(“Vowel” if input(“Enter”) in [‘a’,’e’,’i’,’o’,’u’] else “consonent”)


  • kishore

    c= input()
    vowels=[‘a’,’e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’,’O’,’U’]
    n=len(vowels)-1

    for i in range(n):
    if vowels[i]== c:
    print(c +’ is a vowel’)
    break
    else:
    print(‘Consonant’)
    break


  • nidhisingh151199

    s=input()
    l=[“a”,”e”,”i”,”o”,”u”,”A”,”E”,”I”,”O”,”U”]
    if(s in l):
    print(“Vowel”)
    else:
    print(“Consonant”)


  • Tronic

    letter = input()

    vowels = [‘a’,’e’,’i’,’o’,’u’]

    if letter.lower() in vowels:
    print(letter, ‘is a vowel’)
    else:
    print(letter, ‘is a consonant’)


  • Ashish Kumar

    Char = input()
    #Check if the Char belong to set of Vowels
    if (Char == ‘a’ or Char == ‘e’ or Char == ‘i’ or Char == ‘o’ or Char == ‘u’):
    #if true
    print(“Character is Vowel”)
    else:
    #if false
    print(“Character is Consonant”)
    Do not fetch capital Vowel letter.


  • Ashish Kumar

    char=input()
    if(char in ‘aeiou’):
    print(“Character is Vowel”)
    elif(char in ‘AEIOU’):
    print(“Character is Vowel”)
    else:
    print(“Character is Consonant”)


  • Sanjeet

    #You can try this code
    ch=input(“Enter a character”)
    l=len(ch)
    if(l>1):
    print(ch,”is not a character”)
    elif (ch==’a’ or ch==’A’ or ch==’e’ or ch==’E’ or ch==’i’ or ch==’I’ or ch==’o’ or ch==’O’ or ch==’u’ or ch==’U’):
    print(ch,” is vowel”)
    elif(ch>=’a’ and ch’A’ and ch<'Z'):
    print(ch," is consonant")
    else:
    print(ch, "invalid input")