Python – Test if String contains any Uppercase character
Last Updated :
30 Mar, 2023
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
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
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
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
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
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
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
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #6 : Using replace() and len() methods
Python3
test_str = 'geeksforGeeks'
loweralphabets = "abcdefghijklmnopqrstuvwxyz"
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
print ( "Does String contain uppercase character : " + str (res))
|
Output
The original string is : geeksforGeeks
Does String contain uppercase character : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #7: Using operator.countOf() method
Python3
import operator as op
test_str = 'geeksforGeeks'
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print ( "The original string is : " + str (test_str))
res = False
for ele in test_str:
if op.countOf(uppercase, ele) > 0 :
res = True
break
print ( "Does String contain uppercase character : " + str (res))
|
Output
The original string is : geeksforGeeks
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 = 'geeksforGeeks'
res = bool (re.search(r '[A-Z]' , test_str))
print (res)
|
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 = 'geeksforGeeks'
res = bool (test_str.translate( str .maketrans(' ', ' ', ' abcdefghijklmnopqrstuvwxyz')))
print (res)
|
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 = 'geeksforGeeks'
print ( "The original string is : " + str (test_str))
res = reduce ( lambda x, y: x or y.isupper(), test_str, False )
print ( "Does String contain uppercase character : " + str (res))
|
Output
The original string is : geeksforGeeks
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.
Share your thoughts in the comments
Please Login to comment...