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
- 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 storedch
with the string/statement("Enter a character:")
- In the STDIN section of the code editor the input values are entered.
- We declare the
if
statement with the conditions asch=='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, theprint
function will display the statement"Vowel: "
- If the above condition is not met the
else
statement will be executed and theprint
function will display the statement"Constant: "
- 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.