Python - Test if String contains any Uppercase character - GeeksforGeeks

11 Oct, 2020
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
# Python3 code to demonstrate working of# Test if String contains any Uppercase character# Using isupper() + loop  # initializing stringtest_str = 'geeksforGeeks'  # printing original stringprint("The original string is : " + str(test_str))  res = Falsefor ele in test_str:      # checking for uppercase character and flagging    if ele.isupper():        res = True        break  # printing resultprint("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
# Python3 code to demonstrate working of# Test if String contains any Uppercase character# Using any() + isupper()  # initializing stringtest_str = 'geeksforGeeks'  # printing original stringprint("The original string is : " + str(test_str))  # Using any() to check for any element to be uppercaseres = any(ele.isupper() for ele in test_str)  # printing resultprint("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
# Python3 code to demonstrate working of# Test if String contains any Uppercase character# Using re()import re  # initializing stringtest_str = 'geeksforGeeks'  # printing original stringprint("The original string is : " + str(test_str))  # Using regex to check for any element to be uppercaseres = bool(re.match(r'\w*[A-Z]\w*', test_str))  # printing resultprint("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
# Python3 code to demonstrate working of# Test if String contains any Uppercase character# Using any() + ASCII values  # initializing stringtest_str = 'geeksforGeeks'  # printing original stringprint("The original string is : " + str(test_str))  # Using ascii values check for any element to be uppercaseres = any(ord(ele) != 32 and ord(ele) <=          64 or ord(ele) >= 91 for ele in test_str)  # printing resultprint("Does String contain uppercase character : " + str(res))
Output:
The original string is : geeksforGeeks
Does String contain uppercase character : True

Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary