Python Program to Check Character is Alphabet or Not

In this article, we will learn about Python program to check entered character is alphabet or not.

Getting Started

We need to write a program that checks if input value is alphabet or not. For example,

If input value is 10, output is False.
If input value is e, output is True.
If input value is ^, output is False.

We can check if entered character is alphabet or not in python in various ways –

Simple Approach

At first, let’s see a simple approach in python that checks if value entered by user is alphabet or not –

ch = input('Enter a character')
if len(ch) != 1 :
    print("Invalid Input")
else :
    isAlpha = (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')
    print(isAlpha)

Output:

Enter a character
L
True

Here,

  • We have used len(ch) function that checks if length of entered value is 1 or not. If length is not 1, that means we have entered an invalid value. Otherwise, we move to check if entered value is alphabet or not.
  • (ch >= ‘a’ and ch <= 'z') checks if entered value is small letter (or alphabet) or not.
  • (ch >= ‘A’ and ch <= 'Z') checks if entered value is capital letter (or alphabet) or not.
  • In else block, we first check if entered value is small letter or not. If yes, isAlpha is set to True. Otherwise, condition after or operator is checked and result is set to isAlpha.

Using Function

Now, we want to write a function that returns True if entered value is alphabet. Otherwise, it returns False.
We can easily use above code and write it inside function in python.

def checkAlpha(ch):
    if len(ch) != 1 :
        return None
    else :
        return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')

ch = input('Enter a character')
print(checkAlpha(ch))

Output:

Enter a character
6
False

Using Class

We can also use class and function to check if entered value is alphabet or not. For example,

class AlphaChecker:
    def checkAlpha(self, ch):
        if len(ch) != 1 :
            return None
        else :
            return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')

ch = input('Enter a character')
obj = AlphaChecker()
result = obj.checkAlpha(ch)
print(result)

Output:

Enter a character
8
False

Using ASCII Value

We can also use ASCII value of characters to check if entered value is alphabet or not. So, we will write a function that checks if entered value is alphabet or not.

def checkAlpha(ch):
    if len(ch) != 1 :
        return None
    else :
        return (ord(ch) >= 65 and ord(ch) <= 90) or (ord(ch) >= 97 and ord(ch) <= 122)

ch = input('Enter a character')
print(checkAlpha(ch))

Output:

Enter a character
a
True

ord() function returns ASCII value corresponding to given character.

Using built-in Method – isalpha()

We can use method, isalpha(), to check if a value is alphabet or not. We can do so as below –

def checkAlpha(ch):
    if len(ch) != 1 :
        return None
    else :
        return ch.isalpha()

ch = input('Enter a character')
print(checkAlpha(ch))

Output:

Enter a character
T
True

That’s how we can write a python program to check entered character is alphabet or not.

Checkout other tutorials list – https://tutorialwing.com/python-tutorial/

Leave a Reply