Header Ads Widget

Python program to check whether a character is alphabet or not

 

Python program to check whether a character is alphabet or not

Given a character write a program to  check whether it is alphabet or not . For checking this we have three methods .

Method 1:- By using isalpha() function

Method 2:- By using if-else (character>='A' && character<='Z)

Method 3:- By using Ascii values . (Ascii value of A to Z is 65 to 90 and for a to z is 97 to 122)

Sample input:

5

Sample output:

5 is not a Alphabet

Python program to check whether a character is alphabet or not

The objective of the code is to check whether it is a alphabet or not .

Method 1: (By using isalpha() function)

isalpha(character) :- it will take a argument and return True if it is a character otherwise return false . 

Code:-

ch=input()
if len(ch)>1:
print("Invalid input")
elif ch.isalpha():
print(f"{ch} is a Alphabet")
else:
print(f"{ch} is not a Alphabet")


Output:

z
z is a Alphabet



Method 2: 

ch=input()
if len(ch)>1:
print("Invalid input")
elif (ch>='A' and ch<='Z') or (ch>='a' and ch<='z'):
print(f"{ch} is a alphabet")
else:
print(f"{ch} is not a Alphabet")

Output:

z
z is a Alphabet



Method 3: (By using Ascii values )

ch=input()
if len(ch)>1:
print("Invalid input")
else:
c=ord(ch)
if (c>=65 and c<=90) or (c>=97 and c<=122):
print(f"{ch} is a alphabet")
else:
print(f"{ch} is not a Alphabet")


Output:-

z
z is a Alphabet



Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-