Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check if Character is Vowel or Consonant

Separate a vowel, consonant, or nonletter with string tests. This beginner lesson covers lowercasing, membership, and the single-character input assumption.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 9 of 10
check-if-character-is-vowel-or-consonant.py
1# Take a character as input
2ch = input("Enter a character: ").lower()
3
4# Check if vowel or consonant
5if ch and ch in 'aeiou':
6 print("Vowel")
7elif ch.isalpha():
8 print("Consonant")
9else:
10 print("Not an alphabet")

Output

Enter a character: a
Vowel

Enter a character: b
Consonant

Enter a character: 5
Not an alphabet

In this one

  • Normalize English letter case
  • Use string membership
  • Guard empty text

How it works

  1. 1

    Read ch and lowercase it.

  2. 2

    Require nonempty text before vowel membership.

  3. 3

    Otherwise test alphabetic text or print nonletter feedback.

Edge cases

What happens for A?

What happens for A?

lower produces a, so the result is Vowel.

Why guard an empty response?

Why guard an empty response?

The empty string is a substring of every string; ch must be nonempty before vowel membership is accepted.

What's going on

The intended input is one English letter. lower turns A into a before the program checks membership in the vowel string. That makes the vowel test case-insensitive.

If the vowel test fails, isalpha asks whether the response contains only letters. A letter then becomes Consonant; digits and punctuation reach the last branch. Membership in a string checks substrings, so validating a single character matters before using this as a general text checker.

What happens for A? lower produces a, so the result is Vowel.

Why guard an empty response? The empty string is a substring of every string; ch must be nonempty before vowel membership is accepted.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does lower change?

Pick an answer to check it.

Question 2

What happens for A?

Pick an answer to check it.

Questions people ask

What happens for A?

lower produces a, so the result is Vowel.

Why guard an empty response?

The empty string is a substring of every string; ch must be nonempty before vowel membership is accepted.

What does lower change?

Letter case.