How to check if a character is uppercase in Python?

For example, 'A', 'B', 'C', etc are uppercase letters. This article discusses several methods to check if the character is an uppercase letter in python.

Checking if a character is uppercase using the isupper() method

The isupper() is a built-in method in python that checks if a character is in uppercase or not. It returns a boolean value and doesn't take any arguments. The syntax for the isupper() is given below.

#syntax:
string.isupper()

The following example illustrates the use of isupper() to check if a character is in uppercase or not.

print("character 'A' is uppercase: ", 'A'.isupper())
print("character 'p' is uppercase: ", 'p'.isupper())
print("character 'NDA' is uppercase: ", 'NDA'.isupper())
print("character 'zEbRa' is uppercase: ", 'zEbRa'.isupper())

Output

character 'A' is uppercase:  True
character 'p' is uppercase:  False
character 'NDA' is uppercase:  True
character 'zEbRa' is uppercase:  False

Checking if a character is uppercase by using a range of A-Z

If the ASCII value of a character is greater than or equal to the ASCII value of 'A'(65) and less than or equal to the ASCII value of Z(90), the character is in uppercase. While comparing two characters, the program first converts the character to their ASCII values.

For example,

ch ='M'
if ch >= 'A' and ch <= 'Z':
    print(ch,"is uppercase character")
else:
    print(ch,"is not an uppercase character")

Output

M is uppercase character

Checking if a character is uppercase by using the in operator

The in operator checks the existence of an element in an iterable and returns a boolean value. For example, let us initialize a variable string_1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. The in operator checks if the character exists in string_1. If the character is found if block is executed and the output is printed.

string_1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
print("string_1: ", string_1)
ch = 'M'
if ch in string_1:
    print(ch, "is uppercase character")
else:
    print(ch, "is not uppercase character")

Output

string_1:  ABCDEFGHIJKLMNOPQRSTUVWXYZ
M is uppercase character
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions