Skip to Content

Python – Check If String Contains Uppercase Letters

In this tutorial, we will look at how to check if a string in Python contains any uppercase characters or not with the help of some examples.

How to check if a string contains any uppercase characters?

check if string contains uppercase letters in python

You can use a combination of the Python built-in any() function and the string isupper() function to check if a Python string contains any uppercase characters or not.

The built-in any() function in Python takes an iterable as an argument and returns True if any value in the iterable is truthy (evaluates to True in a boolean context).

So, for each character in the string, check if it is uppercase or not using the string isupper() function and pass the resulting boolean iterable as an argument to the any() function. The following is the syntax –

# check if string contains any uppercase characters
any(ch.isupper() for ch in s)

It returns True if any (that is, at least one) character in the given string is in uppercase.

Examples

Let’s now look at some examples. First, we will create some strings that we will be using throughout this tutorial.

# create strings
s1 = "Avengers"
s2 = "avengers"
s3 = ""

# display the strings
print(s1)
print(s2)
print(s3)

Output:

Avengers
avengers

Here, we created three strings – s1, s2, and s3. The string s1 contains an uppercase character, the string s2 has only lowercase characters, and the string s3 is an empty string.

Let’s use a list comprehension to check if each character in a string is uppercase or not and pass the resulting boolean list as an argument to the any() function.

# check if string contains any uppercase characters
print(any([ch.isupper() for ch in s1]))
print(any([ch.isupper() for ch in s2]))
print(any([ch.isupper() for ch in s3]))

Output:

True
False
False

We get True for s1 as it does contain at least one uppercase letter. We get False for s2 as it does not contain any uppercase letters. Note that we get False for an empty string.

The any() function takes an iterable as an argument, you can also directly pass an iterator which results in an iterable to the any() function (and avoid using the list comprehension).

# check if string contains any uppercase characters
print(any(ch.isupper() for ch in s1))
print(any(ch.isupper() for ch in s2))
print(any(ch.isupper() for ch in s3))

Output:

True
False
False

We get the same results as above.

Now, in case you want to check whether the entire string is in uppercase or not, use the string isupper() function directly on the string.

For example,

# check if all characters in string are uppercase
print("AVENGERS".isupper())
print("Avengers".isupper())

Output:

True
False

We get True for the string “AVENGERS” and False for the string “Avengers”.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.