Check whether a character is an alphabet or not in Python

Understanding Alphabets

An alphabet is any letter from A to Z (both uppercase and lowercase). Any other character is not considered an alphabet.

We will explore three different methods to check whether a character is an alphabet or not in Python.

Method 1: Using if-else

This method checks if the character falls within the range of alphabets.

ch = input("Enter a character: ")
if ('a' <= ch <= 'z') or ('A' <= ch <= 'Z'):
    print(f"{ch} is an alphabet")
else:
    print(f"{ch} is not an alphabet")
            
Input: A
Output: A is an alphabet

Method 2: Using ASCII Values

This method checks the ASCII values of the character.

ch = input("Enter a character: ")
if (65 <= ord(ch) <= 90) or (97 <= ord(ch) <= 122):
    print(f"{ch} is an alphabet")
else:
    print(f"{ch} is not an alphabet")
            
Input: 1
Output: 1 is not an alphabet

Method 3: Using a Function

This method defines a function to check if a character is an alphabet.

def is_alphabet(ch):
    return ('a' <= ch <= 'z') or ('A' <= ch <= 'Z')

def check_alphabet(ch):
    if is_alphabet(ch):
        print(f"{ch} is an alphabet")
    else:
        print(f"{ch} is not an alphabet")

ch = input("Enter a character: ")
check_alphabet(ch)
            
Input: z
Output: z is an alphabet
Strings

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python