In this Python program, we will learn how to check if a given character is an alphabet or a digit. We are using two ways to check if a given character is an alphabet or a digit. 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.
# Python Program to Check if a Character is Alphabet or Digit
# Take the Input from user
ch = input("Enter Your Own 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 Not an Alphabet or a Digit")
Enter Your Own Character: 5
The Given Character 5 is a Digit
Enter Your Own Character: A
The Given Character A is an Alphabet
Enter Your Own Character: ^
The Given Character ^ is Not an Alphabet or a Digit
# Python Program to Check if a character is Alphabet or Digit using isalpha() and isdigit() functions
# Take the Input From the User
ch = input("Enter Your Own 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 Not an Alphabet or a Digit")
Enter Your Own Character : 8
The Given Character 8 is a Digit
Enter Your Own Character : k
The Given Character k is an Alphabet
Enter Your Own Character : #
The Given Character # is Not an Alphabet or a Digit
Comments