Check Vowel And Consonant In Python

The provided Python program is designed to determine whether a given character is a vowel or a consonant. It defines a function called check_vowel_consonant that takes a character as input. The function first converts the character to lowercase to ensure case-insensitivity. It then checks whether the character is present in the set of vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’). If the character is found in the set of vowels, the function returns a string indicating that the character is a vowel; otherwise, it declares the character as a consonant. In the main part of the program, the user is prompted to enter a character. The program then calls the check_vowel_consonant function with the user-inputted character and prints the result to the console, providing information on whether the entered character is a vowel or a consonant. The program offers a straightforward way to analyze individual characters for their vowel or consonant status.


Source code

def check_vowel_consonant(char):
    # Convert the character to lowercase to handle both cases (upper and lower)
    char = char.lower()

    # Check if the character is a vowel
    if char in 'aeiou':
        return f'{char} is a Vowel'
    else:
        return f'{char} is a Consonant'

# Input a character from the user
character = input("Enter a character: ")

# Check and display the result
result = check_vowel_consonant(character)
print(result)

Description

  1. Function Definition:
    • The program defines a function named check_vowel_consonant that takes a single parameter, char, representing a character.
  2. Character Case Normalization:
    • Within the function, the char parameter is converted to lowercase using the lower() method. This step ensures that the program is case-insensitive when checking for vowels.
  3. Vowel Check:
    • The program then checks whether the lowercase character is present in the set of vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) using the in keyword.
  4. Return Statement:
    • If the character is found in the set of vowels, the function returns a string indicating that the character is a vowel. Otherwise, it returns a string declaring the character as a consonant.
  5. User Input:
    • In the main part of the program, the user is prompted to enter a character using the input function. The entered character is stored in the variable named character.
  6. Function Invocation:
    • The program calls the check_vowel_consonant function with the user-inputted character as an argument. The result is stored in the variable named result.
  7. Print Result:
    • Finally, the program prints the result to the console using the print function. The result provides information on whether the entered character is a vowel or a consonant.
  8. Program Execution:
    • When the program is run, the user is prompted to input a character, and based on the function’s logic, it determines and prints whether the entered character is a vowel or a consonant. The conversion to lowercase allows the program to handle both uppercase and lowercase characters uniformly.

Screenshot


Download

By Admin

Leave a Reply

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