Python Program to Check Whether Character is Alphabet

Here we develop a Python program to check whether a character is an alphabet or not. An alphabet is a set of letters or symbols in a fixed order used to represent the basic set of speech sounds of a language, especially the set of letters from A to Z.

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

This python program using the if-else statement to check character is alphabet or not. We will take a character while declaring the variables. Then, check whether the character is an alphabet using the if-else statement. Finally, the result will be displayed on the screen.

# Python program to check whether a character is alphabet or not

# take input
ch = input("Enter any character: ")

# check charater is alphabet or not
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 for the different input values:-

Enter any character: K
K is an Alphabet.

Enter any character: 5
5 is not an Alphabet.

Enter any character: #
# is not an Alphabet.

How to Check if a Character is Alphabet in Python

We are comparing the ASCII values to check whether the character is an alphabet or not. The ord() method is used to find the Unicode value of a character passed as its argument.

# Python program to check whether a character is alphabet or not

# take input
ch = input("Enter any character: ")

# check charater is alphabet or not
if((ord(ch) >= 65 and ord(ch) <= 90) or (ord(ch) >= 97 and ord(ch) <= 122)):
    print(ch, "is an Alphabet.")
else:
    print(ch, "is not an Alphabet.")

Output:-

Enter any character: a
a is an Alphabet.

isalpha Method in Python

This python program also performs the same task but in a different way. In this program, we are using the built-in function to check character is alphabet or not. In Python, isalpha() is a built-in method used for string handling. The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False. This function is used to check if the argument includes only alphabet characters.

The syntax of isalpha() is:

string.isalpha()

isalpha() Parameters:

isalpha() doesn’t take any parameters.

Return Value from isalpha():

  1. True- if all characters in the string are alphabets (can be both lowercase and uppercase).
  2. False- If the string contains 1 or more non-alphabets.
# Python program to check whether a character is alphabet or not

# take input
ch = input("Enter any character: ")

# check charater is alphabet or not
if(ch.isalpha()):
    print(ch, "is an Alphabet.")
else:
    print(ch, "is not an Alphabet.")

Output:-

Enter any character: Z
Z is an Alphabet.

Python Program to check character is Alphabet or Digit

The isdigit() method returns True if all characters in a string are digits. If not, it returns False. The syntax of isdigit() is string.isdigit()

# Python Program to check character is Alphabet or Digit

# take input
ch = input("Enter any character: ")

# check charater is alphabet or digit
if(ch.isalpha()):
    print(ch, "is an Alphabet.")
elif(ch.isdigit()):
    print(ch, "is a Digit.")
else:
    print(ch, "is not an Alphabet or a Digit.")

Output:-

Enter any character: S
S is an Alphabet.

Enter any character: 10
10 is a Digit.

Enter any character: @
@ is not an Alphabet or a Digit.

Also See:- Check if String Starts with Vowel

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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