How to check if a character is upper-case in Python?


In this article, we are going to find out how to check if a character is an upper case in Python..

The first approach is by using the isupper() method. The Python standard library has a built-in method called isupper(). It supports the use of strings and other types of data. It shows whether a string of characters contains only capital letters.

If at least one character is lowercase, it returns FALSE. Otherwise, if every letter in the string is capitalized, it returns TRUE. It doesn't need any parameters.

Example

In the example given below, we are taking 2 strings as input and we are checking if they are uppercased or not using the isupper() method 

str1 = "A"
str2 = "b"

print("Checking if the string '",str1,"' is uppercased or not")
print(str1.isupper())

print("Checking if the string '",str2,"' is uppercased or not")
print(str2.isupper())

Output

The output of the above example is as shown below −

Checking if the string ' A ' is uppercased or not
True
Checking if the string ' b ' is uppercased or not
False

Using Regular Expressions

Regular expressions are used in the second method. Import the re library and install it if it isn't already installed to use it. After importing the re library, we'll use the regular expression ‘[A-Z]’. This will return False if the character is lowercased, otherwise, it returns True.

Example

In the example given below, we are taking 2 characters as input and we are checking if they are upper cased or not using Regular expressions match method 

import re
str1 = "A"
str2 = "b"

print("Checking if the string '",str1,"' is uppercased or not")
print(bool(re.match('[A-Z]', str1)))

print("Checking if the string '",str2,"' is uppercased or not")
print(bool(re.match('[A-Z]', str2)))

Output

The output of the above example is given below −

Checking if the string ' A ' is uppercased or not
True
Checking if the string ' b ' is uppercased or not
False

Using ASCII values

The third method involves the use of ASCII values. We know that the ASCII values for lower case characters start at 97, so we need to check if the character’s ASCII value is less than 97. If the ASCII value is less than 97, true is returned; otherwise, false is returned.

Example

In the example given below, we are taking 2 characters as input and we are checking if they are upper cased using ord() method by comparing ASCII values 

def checkupper(str):
   if ord(str) < 96 :
      return True
   return False 
str1 = 'A'
str2 = 'b'

print("Checking whether",str1,"is upper case")
print(checkupper(str1))

print("Checking whether",str2,"is upper case")
print(checkupper(str2))

Output

The output of the above example is as shown below −

Checking whether A is upper case
True
Checking whether b is upper case
False

Using Comparison

The fourth approach is by direct comparison of the given character. We will check if the character is greater than equal to "A" or less than equal to "Z". If the character lies in this range, then True is returned else False is returned.

Example

In the example given below, we are taking 2 characters as input and checking if they are uppercased or not by comparing them with "A" and "Z" 

def checkupper(str):
   if str >= 'A' and str <= 'Z':
      return True
   else:
      return False
      
str1 = 'A'
str2 = 'b'

print("Checking whether",str1,"is upper case")
print(checkupper(str1))

print("Checking whether",str2,"is upper case")
print(checkupper(str2))

Output

The output of the above example is −

Checking whether A is upper case
True
Checking whether b is upper case
False

Updated on: 26-Aug-2023

34K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements