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 ?

char = 'A'
if char.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 ?

char = 'X'
if char.isalpha() and char.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 ?

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

The following is the output of the above program ?

Vowels: eoe
Consonants: Wlcm

Using List Comprehension

List comprehension provides a concise way to separate vowels and consonants from a string in a single line ?

text = "Python Programming"
vowels = [char for char in text if char.isalpha() and char.lower() in 'aeiou']
consonants = [char for char in text if char.isalpha() and char.lower() not in 'aeiou']

print("Vowels:", ''.join(vowels))
print("Consonants:", ''.join(consonants))
Vowels: yoPoamig
Consonants: Pthn rgrmmn

Conclusion

Use the in operator with 'aeiou' for simple vowel detection. Combine with isalpha() to validate alphabetic characters. List comprehension provides an efficient way to separate vowels and consonants in larger strings.

Updated on: 2026-03-24T16:55:37+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements