Python program to check whether a character is alphabet or not
ADVERTISEMENTS
In this article, you will learn how to check whether a character is an alphabet or not using if-else
and Ternary
operator in the python programming language.
Examples
Input: A
A is an alphabet character.
Input: 9
9 is not an alphabet character.
You should have knowledge of the following topics in python programming to understand these programs:
- Python Ternary operator
- Python
input()
function - Python
str()
function - Python
if-else
condition statement - Python
print()
function
1. Python program to check whether a character is an alphabet or not using if-else statement
# Python program to check whether a character is alphabet or not
print ("Enter a character to check itself: ", end="")
char = str (input ())[0]
if (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z'):
print (char, "is an alphabet character.")
else:
print (char, "is not an alphabet character.")
Output
Enter a character to check itself: A
A is an alphabet character.
2. Python program to check whether a character is an alphabet or not using Ternary operator
# Python program to check whether a character is alphabet or not using conditional operator
print ("Enter a character to check itself: ", end="")
char, flag = str (input ())[0], False
flag = True if (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z') else False
if flag:
print (char, "is an alphabet character.")
else:
print (char, "is not an alphabet character.")
Output
Enter a character to check itself: K
K is an alphabet character.
Explanation
In this program, we have taken input A
from the user then it passes to check this character between alphabetic series.
a, b, ..., y, z
A, B, ..., Y, Z
Although the given character does exist in this series or not if it doesn't in this series then the given character is not alphabetic.