Skip to Content

Python – Check If String Contains Lowercase Letters

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

How to check if a string contains any lowercase characters?

check if a string in python contains any lowercase letters

You can use a combination of the Python built-in any() function and the string islower() function to check if a Python string contains any lowercase 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 lowercase or not using the string islower() function and pass the resulting boolean iterable as an argument to the any() function. The following is the syntax –

# check if string contains any lowercase characters
any(ch.islower() for ch in s)

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

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 lowercase characters, the string s2 has only uppercase characters, and the string s3 is an empty string.

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

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

Output:

True
False
False

We get True for s1 as it does contain at least one lowercase letter. We get False for s2 as it does not contain any lowercase 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 lowercase characters
print(any(ch.islower() for ch in s1))
print(any(ch.islower() for ch in s2))
print(any(ch.islower() 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 lowercase or not, use the string islower() function directly on the string.

For example,

# check if all characters in string are lowercase
print("avengers".islower())
print("Avengers".islower())

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.