In this Python program, we will learn how to check if a character is an alphabet or digit using ASCII values. For checking if a character is an alphabet or digit, 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.
Here is the code of the program to check if a character is an alphabet or digit using ASCII values.
# Python Program to Check character is Alphabet or Digit using ASCII Values
# Take the input from the user
ch = input("Enter Your Own 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 Not an Alphabet or a Digit")
Enter Your Own Character: J
The Given Character J is an Alphabet
Enter Your Own Character: 5
The Given Character 5 is a Digit
Comments