How to detect vowels vs consonants in Python?



In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants.

In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions.

Using Python in Operator

The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false.

To detect whether a letter is a vowel or a consonant, we need to convert the given letter to lowercase, using the Python lower() method, and verify its presence in the string 'aeiou'. 

Example

Let's look at the following example, where we are going to check whether the character is a vowel or not using the in operator.

str1 = 'A'
if str1.lower() in 'aeiou':
   print("It is vowel")
else:
   print("It is consonant")

The output of the above program is as follows -

It is vowel

Using Python isalpha() Method

The isalpha() method is used to check whether a string consists of alphabets or not and returns true if it contains alphabets or false.

We can verify if a given character is an alphabet using this method, and the lower case of it is one of the characters of the letters "aeiou", using the lower() method and the "not in" operator.

Syntax

Following is the syntax of the Python isalpha() method -

str.isalpha()

Example 1

In the following example, we are going to detect whether the character is a vowel or a consonant.

str1 = 'X'
if str1.isalpha() and str1.lower() not in 'aeiou':
   print("It is Consonant")
else:
   print("It is Vowel")

The following is the output of the above program -

It is Consonant

Example 2

In this scenario, we will declare the string and iterate through each character. For each character, we check if it is an alphabetic letter using the .isalpha() method.

If they are alphabetic characters, the character is converted to lowercase and checked against a reference set to determine whether it is a vowel or a consonant.

str1 = "Welcome"
vowels = ""
consonants = ""
for x in str1:
    if x.isalpha():
        if x.lower() in 'aeiou':
            vowels += x
        else:
            consonants += x
print("Vowels:", vowels)
print("Consonants:", consonants)

The following is the output of the above program -

Vowels: eoe
Consonants: Wlcm
Updated on: 2025-06-13T17:39:56+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements