Python Program to Identify Vowels and Consonants Easily

Python Program to Identify Vowels and Consonants Easily

Vowel or Consonant in Python: A Step-by-Step Guide

In this article, we will explore how to determine whether a given character is a vowel or a consonant using different Python methods. This guide is beginner-friendly, and we will cover multiple approaches to achieve this. Before diving into the code, let’s understand what vowels and consonants are:
  • Vowels: Letters like a, e, i, o, u (both uppercase and lowercase).
  • Consonants: All other alphabetic characters excluding vowels.
Let’s dive into the methods with examples and algorithms.

Input and Output Format

Input Format:

  • The input consists of a single character (alphabet).

Output Format:

  • Output a single line stating “Vowel” if the input character is a vowel. Otherwise, output “Consonant”.

Sample Input and Output

Sample Input:
A
Sample Output:
Vowel
Sample Input:
z
Sample Output:
Consonant

Method 1: Using Built-In Functions

Built-In Functions

Algorithm

  1. Take a character input from the user.
  2. Convert the character to lowercase using the lower() function.
  3. Check if the character is in the set of vowels (a, e, i, o, u).
  4. If yes, print “Vowel”; otherwise, print “Consonant”.

Python Code

# Program to check if a character is a vowel or consonant using built-in functions
char = input("Enter the character: ")
if char.lower() in ('a', 'e', 'i', 'o', 'u'):
    print("Vowel")
else:
    print("Consonant")
Input:
E
Output:
Vowel

Method 2: Using Logical Operators

Algorithm

  1. Take a character input from the user.
  2. Use logical operators (or) to compare the input with both uppercase and lowercase vowels.
  3. If the condition matches any vowel, print “Vowel”; otherwise, print “Consonant”.

Python Code

# Program to check if a character is a vowel or consonant using logical operators
char = input("Enter the character: ")
if (char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' or
    char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U'):
    print("Vowel")
else:
    print("Consonant")
Input:
d
Output:
Consonant

Method 3: Using ASCII Values

Algorithm

  1. Take a character input from the user.
  2. Use the ord() function to get the ASCII value of the character.
  3. Compare the ASCII value with those of vowels (both uppercase and lowercase).
  4. If the ASCII value matches any vowel, print “Vowel”; otherwise, print “Consonant”.

Python Code

# Program to check if a character is a vowel or consonant using ASCII values
char = input("Enter the character: ")
if (ord(char) in [65, 69, 73, 79, 85, 97, 101, 105, 111, 117]):
    print("Vowel")
else:
    print("Consonant")
Input:
v
Output:
Consonant

Conclusion

In this guide, we explored three methods to check whether a given character is a vowel or consonant in Python:
  • Using built-in functions
  • Using logical operators
  • Using ASCII values
Each method has its unique approach, and you can choose the one that best fits your use case. By practicing these methods, you’ll strengthen your understanding of Python and its versatile features. Vowel or Consonant in Python
c