Write a Python Program to Check Whether a Character is Alphabet or Not

Hi in this article let's learn how to check if the character entered By the user is an alphabet or not using Python programming language.

There are two ways to solve this problem

1. Using the string built in function is alpha()

The inbuilt isalpha() function is a string function that checks if the character is an English alphabet. If the character is an alphabet then the function returns true.


2. Using the ord() function to check if the ASCII value between a and z

Also there is an ord() function to convert the character into an value so let's convert the character into an ASCII value and if the ASCII value is between a and set so then we conclude that it is an alphabet.



Write a Python Program to Check Whether a Character is Alphabet or Not



Write a Python Program to Check Whether a Character is Alphabet or Not, using isalpha() function

Let's use the input() function to ask the user to enter a character and take the first character as the input letter.  Using the if statement let's check if the letter is an alphabet using inbuilt function isalpha() so if this is true then print character is an alphabet else character is not an alphabet.

letter = input("Enter a Character : ")[0]

if letter.isalpha():
    print("Character is Alphabet")
else:
    print("Character is Not an Alphabet")


OUTPUT 1 : 

Enter a Character : I
Character is Alphabet

OUTPUT 2 :

Enter a Character : 23
Character is Not an Alphabet


 

Write a Python Program to Check Whether a Character is Alphabet or Not, using ord() function

Let's ask a user to enter a character  and take the first character as an input letter. Then check if the input letter is an alphabet or not, let's use the ord() function to calculate the ASCII value of the input later.

Use the if statement to check if ASCII value is between small case  letter a and z and  upper case letter A and Z. If the given letter ASCII value is between A and Z then print the character is an alphabet else print character is not a alphabet.

letter = input("Enter a Character : ")[0]

if ord("a") <= ord(letter) <= ord("z") or ord("A") <= ord(letter) <= ord("Z"):
    print("Character is Alphabet")
else:
    print("Character is Not an Alphabet")


OUTPUT 1 : 

Enter a Character : a
Character is Alphabet

OUTPUT 2 :

Enter a Character : #
Character is Not an Alphabet

 

 

Conclusion : Try to run all the program by yourself and comment down below if you have any issue.

 

Post a Comment

0 Comments