Python program to check whether a character is an alphabet or not
Checking whether a Character is Alphabet or Not in Python ?
Here, we will discuss program to check whether a Character is alphabet or not in python .All characters whether alphabet, digit or special character have ASCII value. Input character from the user will determine if it’s Alphabet, Number or Special character.
Explanation
In C programming language a char type variable can store many different types of characters-
- Alphabets (a, b, c… )
- Digits(1, 2, 3…)
- Special characters(@, %, &…)
These characters are differentiated on the basis of ASCII values :
- between 65 and 90 for upper case(A, B, C…)
- between 97 and 122 for lower case(a, b, c…)
In here we will see how to identify whether a character is alphabet or not using C++ programming language.
Working
- Get user input
- Check if input is between ‘A'(65) – ‘Z'(90) or between ‘a'(96) – ‘z'(122)
- If True print ‘Yes’
- If False print ‘No’
Python code (method 1)
Run
# Python Program to find character is alphabet or not # user input ch = 'z' # basic logic if 'a' <= ch <= 'z' or 'A' <= ch <= 'Z': print("The character", ch, "is an Alphabet") else: print("The character", ch, "is not an Alphabet")
Output
The character z is an Alphabet
Method 2
You can also check the alphabet using the ASCII values of characters like this:
In Python ord() function is used to get ascii value of any character in python.
if 97 <= ord(ch) <= 122 or 65 <= ord(ch) <= 90: print("The inserted character", ch, "is an Alphabet") else: print("The inserted character", ch, "is not an Alphabet")
Python code (method 2)
Run
# Python Program to find character is alphabet or not # user input ch = 'z' # basic logic if 97 <= ord(ch) <= 122 or 65 <= ord(ch) <= 90: print("The character", ch, "is an Alphabet") else: print("The character", ch, "is not an Alphabet")
Output
The character z is an Alphabet
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
def alpha_not(chr):
if chr.isalpha():
print(f”{chr} is alphabet”)
else:
print(f”{chr} is not alphabet”)
chr = input(“enter a chr: “)
alpha_not(chr)
a=input(“”)
s=”A”,”E”,”I”,’O’,’U’,’a’,’e’,’i’,’o’,’u’
q=0
for i in s:
if(a==i):
q=q+1
if(q>=1):
print(“vowel”)
else:
print(“not a vowel”)
s=input()
if(ord(s)>=65 and ord(s)=97 and ord(s)<=122):
print("Alphabet")
else:
print("Nor Alphabet")
letter = input()
if letter == letter.upper():
print(letter, ‘is alphabet’)
else:
print(letter, ‘not a alphabet’)
n=input()
if n.isalpha():
print(“yes”)
else:
print(“no”)