In this python tutorial, you will learn how to Check if a Character is an Alphabet or not using the if and else statements along with the equality operators of the python programming language.
How to Check if a Character is an Alphabet or not?
Let’s take a look at the source code , here the values are given as input by the user in the code, the <= lesser than equal to operator and the >= greater than equal to operator along with the if and else statements carry out the function.
RUN CODE SNIPPET#Python program to Check if a Character is an Alphabet or not ch = input("Enter a character: ") if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')): print("\n{0} is an Alphabet".format(ch)) else: print("\n{0} is not an Alphabet".format(ch))
INPUT:
Buddy
OUTPUT:
Enter a character: Buddy is an Alphabet
- Here we give the user the option to enter the values and the input values are scanned using the
input
function and are stored in the variablech
with the statements/strings("Enter a character:" )
. - In the STDIN section of the code editor the input values are entered.
- We declare the
if
statement with the conditions as(ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')
followed by a colon:
If the conditions are met, theprint
function will display the statement("\n{0} is an Alphabet".format(ch))
. - If the above condition is not met the
else
function will be executed and theprint
function will display the statement("\n{0} is not an Alphabet".format(ch))
. - In the above lines of code, the variables
{0}
will hold the value ofch
where theformat
function helps in variable substitution and data formatting.
NOTE:
- The >= greater than and equal to operator along with the <= greater than and equal to operator is a comparison operator which returns True if the condition is satisfied, if not it returns false.
- The input() function allows a user to insert a value into a program, it returns a string value.
- 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, elif 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.