Given a String, Test if it contains any uppercase character.
Input : test_str = 'neveropen' Output : False Explanation : No uppercase character in String.
Input : test_str = 'LazyroarforgEeks' 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 string test_str = 'LazyroarforGeeks' # printing original string print ( "The original string is : " + str (test_str)) res = False for ele in test_str: # checking for uppercase character and flagging if ele.isupper(): res = True break # printing result print ( "Does String contain uppercase character : " + str (res)) |
Output:
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using any() + isupper()
In this, we use any() to check for any character if it is an uppercase character.
Python3
# Python3 code to demonstrate working of # Test if String contains any Uppercase character # Using any() + isupper() # initializing string test_str = 'LazyroarforGeeks' # printing original string print ( "The original string is : " + str (test_str)) # Using any() to check for any element to be uppercase res = any (ele.isupper() for ele in test_str) # printing result print ( "Does String contain uppercase character : " + str (res)) |
Output:
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time Complexity: O(n)
Auxiliary Space: O(1)
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 string test_str = 'LazyroarforGeeks' # printing original string print ( "The original string is : " + str (test_str)) # Using regex to check for any element to be uppercase res = bool (re.match(r '\w*[A-Z]\w*' , test_str)) # printing result print ( "Does String contain uppercase character : " + str (res)) |
Output:
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time Complexity: O(n)
Auxiliary Space: O(1)
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 string test_str = 'LazyroarforGeeks' # printing original string print ( "The original string is : " + str (test_str)) # Using ascii values check for any element to be uppercase res = any ( ord (ele) ! = 32 and ord (ele) < = 64 or ord (ele) > = 91 for ele in test_str) # printing result print ( "Does String contain uppercase character : " + str (res)) |
Output:
The original string is : LazyroarforGeeks 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
# Python3 code to demonstrate working of # Test if String contains any Uppercase character # initializing string test_str = 'LazyroarforGeeks' uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # printing original string print ( "The original string is : " + str (test_str)) res = False for ele in test_str: if (ele in uppercase): res = True break # printing result print ( "Does String contain uppercase character : " + str (res)) |
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #6 : Using replace() and len() methods
Python3
# Python3 code to demonstrate working of # Test if String contains any Uppercase character # initializing string test_str = 'LazyroarforGeeks' loweralphabets = "abcdefghijklmnopqrstuvwxyz" # printing original string print ( "The original string is : " + str (test_str)) res = False for i in loweralphabets: test_str = test_str.replace(i,"") if ( len (test_str)> = 1 ): res = True # printing result print ( "Does String contain uppercase character : " + str (res)) |
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #7: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of # Test if String contains any Uppercase character import operator as op # initializing string test_str = 'LazyroarforGeeks' uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # printing original string print ( "The original string is : " + str (test_str)) res = False for ele in test_str: if op.countOf(uppercase, ele) > 0 : res = True break # printing result print ( "Does String contain uppercase character : " + str (res)) |
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method#8: Using re.search() function from the re module
Python3
import re test_str = 'LazyroarforGeeks' res = bool (re.search(r '[A-Z]' , test_str)) print (res) #This code is contributed by Vinay Pinjala. |
True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method#9: Using the str.translate() method with the str.maketrans() method:
Algorithm:
- Create a translation table that removes all lowercase alphabets using the str.maketrans() method.
- Apply the translation table on the input string using the translate() function.
- Check if the resulting string is empty.
- If the string is empty, return True (input string contains only uppercase alphabets), otherwise return False (input string contains at least one lowercase alphabet).
Python3
test_str = 'LazyroarforGeeks' res = bool (test_str.translate( str .maketrans(' ', ' ', ' abcdefghijklmnopqrstuvwxyz'))) print (res) #this code contributed by tvsk |
True
Time complexity:
The str.maketrans() method has a time complexity of O(1) as it creates a translation table for a fixed set of characters. The translate() function has a time complexity of O(n) where n is the length of the input string. Therefore, the overall time complexity of the algorithm is O(n).
Auxiliary space:
The algorithm only creates a translation table and a resulting string, both of which have a constant size. Therefore, the auxiliary space complexity of the algorithm is O(1).
Method #10 : Using reduce() function from the functools module:
Algorithm:
- Import the reduce() function from the functools module.
- Define the input string.
- Use the reduce() function to iterate over each character in the string and apply the lambda function.
- The lambda function checks if the current character is uppercase and updates the accumulator accordingly.
- The reduce() function returns the final accumulator value.
- Use the bool() function to convert the result into a boolean value.
Python3
from functools import reduce test_str = 'LazyroarforGeeks' # printing original string print ( "The original string is : " + str (test_str)) # Using reduce() to check if any element is uppercase res = reduce ( lambda x, y: x or y.isupper(), test_str, False ) # printing result print ( "Does String contain uppercase character : " + str (res)) #This code is contributed by Rayudu |
The original string is : LazyroarforGeeks Does String contain uppercase character : True
Time complexity: The time complexity of the reduce() function depends on the size of the input string. In the worst case, the lambda function will be called for each character in the string. Therefore, the time complexity of this algorithm is O(n) where n is the length of the input string.
Auxiliary Space: The space complexity of this algorithm is O(1) because we are only using a constant amount of extra space to store the input string and the accumulator value.