In this Python program, we will learn how to check whether a character is an alphabet, digit, or special character using ASCII values. For checking if a character is an alphabet, digit or special character, we will use if...elif...else
statement and ord()
function to convert a character to an ASCII value and compare it with predefined ASCII value range which starts from 0 to 127. For checking, if a character is an alphabet or digit, enter the alphabet from a-z or A-Z and for digit, enter the value between 0-9 and for Special character enter the value such as #, $, %, ^, &, etc.
Here is the code of the program to check whether a character is an alphabet, digit, or special character using ASCII values.
# Python Program to check whether a character is an Alphabet ,Digit or Special Character using ASCII Values
# Take the Input from The User
ch = input("Enter any Character: ")
if(ord(ch) >= 48 and ord(ch) <= 57):
print("The Given Character", ch, "is a Digit")
elif((ord(ch) >= 65 and ord(ch) <= 90) or (ord(ch) >= 97 and ord(ch) <= 122)):
print("The Given Character", ch, "is an Alphabet")
else:
print("The Given Character", ch, "is a Special Character")
Enter any Character: 5
The Given Character 5 is a Digit
Enter any Character: G
The Given Character G is an Alphabet
Enter any Character: %
The Given Character % is a Special Character
Comments