ScanSkill

Check Vowel or Consonant in Python

In this, you'll get to learn how to check vowel or consonant in Python. And the character is input from the user. i.e. Let's say if the user entered a character e, then the output should be similar like -- “e is a Vowel.”.

Vowel letters are — a, e, i, o, u

This covers four different approaches:

  • Check vowel or consonant in Python using if-else
  • Check vowel or consonant in Python using list
  • Check vowel or consonant in Python using function
  • Check vowel or consonant in Python using class

Prerequisites

Examples

Check vowel or consonant in Python using if-else

This is an example to check whether a character entered by the user is a vowel or consonant. In this, a character is taken from user input and then applied conditions using if else to check if it is a vowel. (Vowels — a, e, i, o, u or A, E, I, O, U)

print("Enter the Character: ")
ch = input()

if len(ch) >= 2:
    print("Invalid input!")
else:
    if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u':
        print(f"{ch} is a Vowel.")
    elif ch=='A' or ch=='E' or ch=='I' or ch=='O' or ch=='U':
        print(f"{ch} is a Vowel.")
    else:
        print(f"{ch} is a Consonant.")

Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.

And, ==(is equal) assignment operator checks if the value of ch is equal to mentioned value.

Output:

Enter the Character: 
g
g is a Consonant.
Enter the Character: 
u
u is a Vowel.
Enter a Character: 
ce
Invalid input!

Check vowel or consonant in Python using list

This example is to check a character or letter entered by the user is a vowel or consonant using a list.

print("Enter the Character: ")
ch = input()

vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']

if len(ch)>=2:
    print("Invalid input!")
else:
    if ch in vowels:
        print(f"{ch} is a Vowel.")
    else:
        print(f"{ch} is a Consonant.")

Here, a list of vowels is used(uppercase and lowercase) is used to differentiate the vowels and consonants.

Output:

Enter the Character: 
g
g is a Consonant.
Enter the Character: 
u
u is a Vowel.

Check vowel or consonant in Python using function

This also does the same task as the above programs but using a user-defined function method.

def checkVowelorConsonant(c):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    if c in vowels:
        print(f"{c} is a Vowel.")
    else:
        print(f"{c} is a Consonant.")

print("Enter a Character: ")
ch = input()

if len(ch)>=2:
    print("Invalid input!")
else:
    checkVowelorConsonant(ch)

Here, a function checkVowelorConsonant() is defined to check vowel or consonant that takes an argument. Then the function is called after the user input is taken by passing the input character.

Output:

Enter the Character: 
g
g is a Consonant.
Enter the Character: 
u
u is a Vowel.

Check vowel or consonant in Python using class

class VowelOrConsonant:
    def checkVowelorConsonant(self, c):
        vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
        if c in vowels:
            print(f"{c} is a Vowel.")
        else:
            print(f"{c} is a Consonant.")

print("Enter a Character: ")
ch = input()

if len(ch)>=2:
    print("Invalid input!")
else:
    obj1 = VowelOrConsonant()
    res = obj1.checkVowelorConsonant(ch)

Here, all properties of the class named VowelOrConsonant get assigned to an object named obj1. Then the object can be used to access the member function of the class VowelOrConsonant using the dot (.) operator. (vc_obj.checkVowelorConsonant(ch))

Output:

Enter the Character: 
g
g is a Consonant.
Enter the Character: 
u
u is a Vowel.
Enter a Character: 
ce
Invalid input!

Conclusion

In this, we discussed different examples for different methods to find whether the character entered by the user is Vowel or Consonant -- using if else, using list, using a function, and using class.