Check a consonant

EasyIf-Else Programs · 33 of 64

Write a Python program to check whether a letter is a consonant.

Advertisement
Input
"B", "e", "7"
Output
B is a consonant
e is not a consonant
7 is not a consonant

Approach

  1. A consonant is a letter (isalpha()) that is not a vowel.

Solution

Python
for ch in ["B", "e", "7"]:    if ch.isalpha() and ch.lower() not in "aeiou":        print(ch, "is a consonant")    else:        print(ch, "is not a consonant")
Output
B is a consonant
e is not a consonant
7 is not a consonant

Line by line

Line 2

Checking isalpha() first stops digits and symbols from being called consonants.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs