In Python, we can check if a string contains uppercase characters by checking each letter to see if that letter is uppercase in a loop.

def checkStrContainsUpper(string):
    for x in string:
        if x == x.upper():
            return True
    return False

print(checkStrContainsUpper("all letters here are lowercase"))
print(checkStrContainsUpper("We Have some uppercase Letters in this One."))

#Output:
False
True

When processing strings in a program, it can be useful to know if we have uppercase or lowercase characters. Using Python, we can easily check if string contains uppercase characters with the help of the Python upper() function.

To check if a string contains uppercase, we just need to loop over all letters in the string until we find a letter that is equal to that letter after applying the upper() function.

Below is a Python function which will check if a string contains uppercase characters.

def checkStrContainsUpper(string):
    for x in string:
        if x == x.upper():
            return True
    return False

print(checkStrContainsUpper("all letters here are lowercase"))
print(checkStrContainsUpper("We Have some uppercase Letters in this One."))

#Output:
False
True

How to Check if String Contains Lowercase in Python

We can also check if a string contains lowercase characters in Python very easily.

To check if a string contains lowercase letters in Python, we can adjust our function that we defined above to use the Python lower() function, instead of the upper() function.

Below is a Python function which will check if a string contains lowercase characters.

def checkStrContainsLower(string):
    for x in string:
        if x == x.lower():
            return True
    return False

print(checkStrContainsLower("ALL THE LETTERS ARE UPPERCASE"))
print(checkStrContainsLower("We Have some uppercase Letters in this One."))

#Output:
False
True

Hopefully this article has been useful for you to learn how to check if a string contains uppercase characters in Python.

Categorized in:

Python,

Last Update: March 18, 2024