Example HTML page

A Python program to check whether an input character is an Alphabet or not

In today’s post i am going to describe a python program that checks whether the user given input is an alphabet or not. If that input character is an alphabet it displays the message that the “character is an Alphabet” else it displays that the “character is not an Alphabet”.

Program

ch = input("Enter a character: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
    print(ch, "is an Alphabet")
else:
    print(ch, "is not an Alphabet")

Output

Enter a character: s

s is an Alphabet

 

Enter a character: S

S is an Alphabet

 

Enter a character: 9

9 is not an Alphabet

In this program, a character is given as an input and is stored in the variable ‘ch’. By using if condition we can check whether the entered element lies in the range of lowercase or uppercase alphabets. If the condition becomes true it displays message as “is an Alphabet” otherwise it prints “is not an Alphabet”.

We can also generate a list of the letters in alphabets. For this first we have to find the ASCII code for both lowercase and uppercase alphabets by using ord( ) function. The below examples shows you how to generate a list of the letters in alphabets in both lowercase and uppercase letters..

#Create alphabet list of lowercase letters

>>> ord(‘a’)                                  #ASCII code for lowercase alphabets

97

>>> ord(‘z’)

122

 Program

alphabet = [ ]
for letter in range(97,123):
    alphabet.append(chr(letter))
print(alphabet)

Output

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]

#Create alphabet list of uppercase letters

>>> ord(‘A’)                   #ASCII code for uppercase alphabets

65

>>> ord(‘Z’)

90

 Program

alphabet = [ ]
for letter in range(65, 91):
    alphabet.append(chr(letter))
print(alphabet)

Output

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’]

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest