In this python program, we will learn how to check if a given character is an alphabet or a digit or a special character. In this program, we will use two ways to check if a given character is an alphabet or a digit or a special character. First by checking if the given character is in between a-z or A-Z and 0-9 and the other way to check is by using built-in python function: isalpha()
and isdigit()
.
Here is the source code of the program to check if a given character is an alphabet or a digit or a special character.
# Python Program to check if a Character is an Alphabet, Digit or Special Character
# Take the Input from the user
ch = input("Enter any Character: ")
if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')):
print("The Given Character", ch, "is an Alphabet")
elif(ch >= '0' and ch <= '9'):
print("The Given Character", ch, "is a Digit")
else:
print("The Given Character", ch, "is a Special Character")
Enter any Character: 4
The Given Character 4 is a Digit
Enter any Character: j
The Given Character j is an Alphabet
Enter any Character: $
The Given Character $ is a Special Character
# Python Program to Check if a character is an Alphabet, Digit or Special Character
# using isalpha(), isdigit() functions
# Take the Input from User
ch = input("Enter any Character: ")
if(ch.isdigit()):
print("The Given Character", ch, "is a Digit")
elif(ch.isalpha()):
print("The Given Character", ch, "is an Alphabet")
else:
print("The Given Character", ch, "is a Special Character")
Enter any Character: d
The Given Character d is an Alphabet
Enter any Character: 6
The Given Character 6 is a Digit
Enter any Character: *
The Given Character * is a Special Character
Comments