Check If a String Contains Only Upper Case Letters in Python



Checking for Uppercase Letters in Python

A string is considered to contain only upper-case letters if all its characters are alphabets and each one is in upper case (from 'A' to 'Z').

Python provides various ways to check this, such as using the isupper() method, regular expressions, or manually checking each character.

Using isupper() Method

The isupper() method returns True if all the alphabetic characters in the string are uppercase and there is at least one alphabet. If the string contains any lowercase letters or non-alphabetic characters, it returns False.

Example: All Uppercase Letters

In the following example, the string contains only uppercase alphabetic characters, so the isupper() method returns True -

str1 = "PYTHONRULES"

print("The given string is")
print(str1)

print("Checking if the string contains only uppercase letters")
print(str1.isupper())

The output is -

The given string is
PYTHONRULES
Checking if the string contains only uppercase letters
True

Example: Mixed Characters

In the following example, the string includes lowercase letters and digits, so the isupper() method returns False -

str2 = "Python123"

print("The given string is")
print(str2)

print("Checking if the string contains only uppercase letters")
print(str2.isupper())

The output is -

The given string is
Python123
Checking if the string contains only uppercase letters
False

Using Regular Expressions

You can use the re.fullmatch() function to check if a string contains only upper-case letters. This function accepts a pattern and a string as parameters and verifies whether the given string is in the specified pattern.

The pattern [A-Z]+ matches only strings that contain one or more uppercase letters.

Example: Using re.fullmatch() function

In the following example, the string contains only uppercase letters and matches the regular expression pattern -

import re

str3 = "HELLOWORLD"

print("The given string is")
print(str3)

print("Checking if the string contains only uppercase letters")
print(bool(re.fullmatch(r"[A-Z]+", str3)))

The output is as follows -

The given string is
HELLOWORLD
Checking if the string contains only uppercase letters
True

Example: String with Invalid Characters 

In the following example, the string contains a space and lowercase letters, so the program prints False -

import re

str4 = "HELLO World"

print("The given string is")
print(str4)

print("Checking if the string contains only uppercase letters")
print(bool(re.fullmatch(r"[A-Z]+", str4)))

The output is as shown below -

The given string is
HELLO World
Checking if the string contains only uppercase letters
False

Using Character-wise Check

The isupper() method checks for uppercase, and the isalpha() method checks for lowercase. The all() function returns true if all of its elements are true.

You can loop through each character in the string and verify if it is an uppercase alphabet using the isupper() and isalpha() methods. If all characters result in true for both methods, the string is valid.

Example: Using isupper() and isalpha()

In the following example -

def is_all_uppercase(string):
   result = all(c.isupper() and c.isalpha() for c in string)
   return result

str5 = "UPPERCASEONLY"

print("The given string is")
print(str5)

print("Checking if the string contains only uppercase letters")
print(is_all_uppercase(str5))

The result obtained is -

The given string is
UPPERCASEONLY
Checking if the string contains only uppercase letters
True
Updated on: 2025-06-09T07:44:48+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements