How do you check if a string has an uppercase letter python?

Given a String, Test if it contains any uppercase character.

Input : test_str = ‘geeksforgeeks’ 
Output : False 
Explanation : No uppercase character in String.

Input : test_str = ‘geeksforgEeks’ 
Output : True 
Explanation : E is uppercase in String. 

Method #1 : Using loop + isupper()

In this, we iterate for each character in String, check for uppercase using isupper(), if found, String is flagged as True.

Python3

test_str = 'geeksforGeeks'

print("The original string is : " + str(test_str))

res = False

for ele in test_str:

    if ele.isupper():

        res = True

        break

print("Does String contain uppercase character : " + str(res))

Output:

The original string is : geeksforGeeks
Does String contain uppercase character : True

Method #2 : Using any() + isupper()

In this, we use any() to check for any character if it is a uppercase character.

Python3

test_str = 'geeksforGeeks'

print("The original string is : " + str(test_str))

res = any(ele.isupper() for ele in test_str)

print("Does String contain uppercase character : " + str(res))

Output:

The original string is : geeksforGeeks
Does String contain uppercase character : True

Method #3 : Using regex()

Appropriate regex can be used to perform this task. This checks for any uppercase in the String.

Python3

import re

test_str = 'geeksforGeeks'

print("The original string is : " + str(test_str))

res = bool(re.match(r'\w*[A-Z]\w*', test_str))

print("Does String contain uppercase character : " + str(res))

Output:

The original string is : geeksforGeeks
Does String contain uppercase character : True

Method #4 : Using any() + ASCII values

Checks for each character to be in pool of capital case of ASCII values.

Python3

test_str = 'geeksforGeeks'

print("The original string is : " + str(test_str))

res = any(ord(ele) != 32 and ord(ele) <=

          64 or ord(ele) >= 91 for ele in test_str)

print("Does String contain uppercase character : " + str(res))

Output:

The original string is : geeksforGeeks
Does String contain uppercase character : True

The Time and Space Complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #5: Without any built-in methods

Python3

test_str = 'geeksforGeeks'

uppercase="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

print("The original string is : " + str(test_str))

res = False

for ele in test_str:

    if(ele in uppercase):

        res=True

        break

print("Does String contain uppercase character : " + str(res))

Output

The original string is : geeksforGeeks
Does String contain uppercase character : True


How do I check if a string contains uppercase?

Traverse the string character by character from start to end. Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. If the ASCII value lies in the range of [97, 122], then it is a lowercase letter.

How do you identify an uppercase letter in Python?

To check if a character is upper-case, we can simply use isupper() function call on the said character.

How do you check if something is uppercase or lowercase in python?

Use islower() and isupper() to check if a string is upper, lower, or mixed case. Call str. islower() with str as a string to determine if str is all lowercase.