Press ESC to close

How to Check Whether a Number is Vowel or Consonent in Python

Python is a versatile and most popular programming language. It is easy to learn and always known for its simplicity and readability. This is a simple program written in python to difference between vowel letters and consonants. Python programming language is mostly used for machine learning and automation. It’s widely used in various fields such as web development, data analysis, artificial intelligence, scientific computing, and automation, making it one of the most popular languages in the world today.

def check_vowel_or_consonant(char):
    vowels = 'aeiouAEIOU'
    
    if len(char) == 1 and char.isalpha():
        if char in vowels:
            return f"{char} is a vowel."
        else:
            return f"{char} is a consonant."
    else:
        return "Please enter a single alphabetic character."

# Example usage
char = input("Enter a character: ")
result = check_vowel_or_consonant(char)
print(result)

How the code works:

  1. vowels variable: It contains both uppercase and lowercase vowels.
  2. Input validation: The function checks if the input is a single alphabetic character using len(char) == 1 and char.isalpha().
  3. Vowel check: If the character is found in the vowels string, it is classified as a vowel.
  4. Consonant check: If the character is not a vowel, it’s considered a consonant.

Error handling: If the input is not a single alphabetic character, the function prompts the user to enter a valid input.

Leave a Reply

Your email address will not be published. Required fields are marked *