How to Check if a String is All Uppercase in Python?

In this tutorial, I will explain how to check if a string is all uppercase in Python. As a Python developer creating an application for a customer service platform for one of my clients in the USA, I encountered a requirement to check string is all uppercase. After exploring Python’s string methods, I discovered a simple and efficient way to achieve this task, which I will explain below.

Check if a String is All Uppercase in Python

Python provides various ways to check if a string is All uppercase in Python. Let us see some important methods.

Read How to Check if a String Ends with a Pattern in Python?

1. Use the isupper() Method

Python provides a built-in method isupper() that can be used to check if all characters in a string are uppercase.

Example:

text = "HELLO AMERICA"
if text.isupper():
    print("The string is all uppercase.")
else:
    print("The string is not all uppercase.")

Output:

The string is all uppercase.

I have executed the above example code and added the screenshot below.

String is All Uppercase in Python

This method returns True if all alphabetic characters in the string are uppercase, and there are no lowercase letters. Non-alphabetic characters are ignored.

Read How to Check if a String Starts with a Specific Substring in Python?

Handle Mixed Content

The isupper() method only checks alphabetic characters. If the string contains numbers, punctuation, or spaces, they do not affect the result.

text = "WELCOME TO NEW YORK 2024!"
if text.isupper():
    print("The string is all uppercase.")
else:
    print("The string is not all uppercase.")

Output:

The string is all uppercase.

I have executed the above example code and added the screenshot below.

Check if a String is All Uppercase in Python

Read How to Check if a String is an Integer or Float in Python?

Check Each Character Individually

Sometimes, you may want more control over the validation process. You can iterate through each character in the string and check if it is uppercase using the isupper() method.

Example:

text = "CALIFORNIA"
all_upper = True

for char in text:
    if char.isalpha() and not char.isupper():
        all_upper = False
        break

if all_upper:
    print("The string is all uppercase.")
else:
    print("The string is not all uppercase.")

Output:

The string is all uppercase.

I have executed the above example code and added the screenshot below.

How to Check if a String is All Uppercase in Python

In this example, we manually check each character to determine if the string is all uppercase.

Read How to Check if a String is Empty or NaN in Python?

2. Use Regular Expressions

For more complex scenarios, regular expressions (regex) can be a powerful tool. Python’s re module allows you to use regex to match patterns in strings.

Example:

import re

text = "WASHINGTON DC"
pattern = r'^[A-Z\s]+$'

if re.match(pattern, text):
    print("The string is all uppercase.")
else:
    print("The string is not all uppercase.")

Output:

The string is all uppercase.

I have executed the above example code and added the screenshot below.

Check if a String is All Uppercase in Python re

In this example, the regex pattern ^[A-Z\s]+$ matches strings that contain only uppercase letters and spaces.

Read Convert Binary to Decimal in Python

Application: User Input Validation

Imagine you are developing a web application where users need to enter their state names in uppercase. You can use the isupper() method to validate the input.

Example:

def validate_state_name(state_name):
    if state_name.isupper():
        return "Valid state name."
    else:
        return "Invalid state name. Please enter the state name in uppercase."

# User input
user_input = "TEXAS"
print(validate_state_name(user_input))

user_input = "Texas"
print(validate_state_name(user_input))

Output:

Valid state name.
Invalid state name. Please enter the state name in uppercase.

Read How to Compare Strings in Python?

Common Issues and Best Practices

Let us see some common pitfalls and best practices.

Issue: Ignore Non-Alphabetic Characters

Remember that the isupper() method ignores non-alphabetic characters. If your validation requires that the entire string (including numbers and punctuation) be in uppercase, you need to handle these cases separately.

Read Find the First Number in a String in Python

Best Practice: Combine Methods

For robust validation, consider combining multiple methods. For example, use isupper() for initial checks and regex for more complex patterns.

Example of Combined Methods

import re

def validate_string(text):
    if text.isupper() and re.match(r'^[A-Z0-9\s]+$', text):
        return "The string is valid and all uppercase."
    else:
        return "The string is invalid or not all uppercase."

# Test cases
print(validate_string("NEW YORK 2024"))
print(validate_string("New York 2024"))
print(validate_string("NEW YORK!"))

Output:

The string is valid and all uppercase.
The string is invalid or not all uppercase.
The string is invalid or not all uppercase.

Read How to Reverse a String in Python?

Conclusion

In this tutorial, I have explained how to check if a string is all uppercase in Python. I explained isupper() method which is used in simple conditions, For more complex scenarios, you can use character iteration or regular expressions. I also discussed applications and some common issues and best practices.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.