GeeksforGeeks App
Open App
Browser
Continue

Python – Test if String contains any Uppercase character

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 string``test_str ``=` `'geeksforGeeks'` `# 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 : 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

 `# Python3 code to demonstrate working of``# Test if String contains any Uppercase character``# Using any() + isupper()` `# initializing string``test_str ``=` `'geeksforGeeks'` `# 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 : 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

 `# Python3 code to demonstrate working of``# Test if String contains any Uppercase character``# Using re()``import` `re` `# initializing string``test_str ``=` `'geeksforGeeks'` `# 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 : 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

 `# Python3 code to demonstrate working of``# Test if String contains any Uppercase character``# Using any() + ASCII values` `# initializing string``test_str ``=` `'geeksforGeeks'` `# 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 : 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

 `# Python3 code to demonstrate working of``# Test if String contains any Uppercase character` `# initializing string``test_str ``=` `'geeksforGeeks'``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))`

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

 `# Python3 code to demonstrate working of``# Test if String contains any Uppercase character` `# initializing string``test_str ``=` `'geeksforGeeks'``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))`

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

 `# Python3 code to demonstrate working of``# Test if String contains any Uppercase character``import` `operator as op``# initializing string``test_str ``=` `'geeksforGeeks'``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))`

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)``#This code is contributed by Vinay Pinjala.`

Output

`True`

Time Complexity: O(N)

Auxiliary Space : O(1)

Method#9: Using the str.translate() method with the str.maketrans() method:

Algorithm:

1. Create a translation table that removes all lowercase alphabets using the str.maketrans() method.
2. Apply the translation table on the input string using the translate() function.
3. Check if the resulting string is empty.
4. 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)``#this code contributed by tvsk`

Output

`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:

1. Import the reduce() function from the functools module.
2. Define the input string.
3. Use the reduce() function to iterate over each character in the string and apply the lambda function.
4. The lambda function checks if the current character is uppercase and updates the accumulator accordingly.
5. The reduce() function returns the final accumulator value.
6. Use the bool() function to convert the result into a boolean value.

Python3

 `from` `functools ``import` `reduce`  `test_str ``=` `'geeksforGeeks'` `# 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`

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.

My Personal Notes arrow_drop_up