Python Program to Check if a Character is a Vowel or Consonant

In this python tutorial, you will learn how to Check if a Character is a Vowel or Consonant using the if and else statements along with the == equality operator of the python programming language.

How to Check if a Character is a Vowel or Consonant?

Let’s take a look at the source code , here the values are given as input by the user in the code, the == equality operator along with the if and else statements carry out the function.

RUN CODE SNIPPET
# Python program to check character is vowel or consonant

# the user gives the input
ch = input("Enter a character: ")

# check vowel or constant and display result
if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I' 
      or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
    print("\nVowel: ", ch)
else:
    print("\nConsonant: ", ch)

INPUT:

O

OUTPUT:

Enter a character: 
Vowel: o
  1. Here we give the user the option to enter the values and the input values are scanned using the input function and  the variables are stored ch with the string/statement ("Enter a character:")
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the if statement with the conditions as ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I' or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u' followed by a colon :. If the conditions are met, the print function will display the statement "Vowel: "
  4.  If the above condition is not met the else statement will be executed and the print function will display the statement "Constant: "
  5. The statements are followed by a comma after which the declared variable is given, that is ch.

NOTE:

  • The == equality is a comparison operator which returns True is the two items are equal and returns False if not equal.
  • The vowels (a, e, i, o, u)both in Capitals and small case are assigned the comparison operator, which is the condition that we have put forth.
  • The variables are enclosed in single quotes which is separated by ‘or’ and followed by a colon ‘:’.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.

Leave A Reply

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

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...