Last updated: Apr 9, 2024
Reading timeยท3 min

To check if a string contains any uppercase letters:
str.isupper() method to check if each character is uppercase.my_str = 'Bobby' contains_uppercase = any(char.isupper() for char in my_str) print(contains_uppercase) # ๐๏ธ True if contains_uppercase: # ๐๏ธ this runs print('The string contains uppercase letters') else: print('The string does NOT contain any uppercase letters') # -------------------------------------------------- # โ Extract the uppercase letters from a string my_str = 'BOBBYhadz123' only_upper = ''.join(char for char in my_str if char.isupper()) print(only_upper) # ๐๏ธ BOBBY only_upper = [char for char in my_str if char.isupper()] print(only_upper) # ๐๏ธ ['B', 'O', 'B', 'B', 'Y']

We used a generator expression to iterate over the string.
On each iteration, we use the str.isupper() method to check if the current
character is an uppercase letter.
The str.isupper()
method returns True if all cased characters in the string are uppercase and
the string contains at least one cased character, otherwise False is returned.
my_str = 'BOBBYHADZ.COM' all_uppercase = my_str.isupper() print(all_uppercase) # ๐๏ธ True
The any function
takes an iterable as an argument and returns True if any element in the
iterable is truthy.
my_str = 'Bobby' contains_uppercase = any(char.isupper() for char in my_str) print(contains_uppercase) # ๐๏ธ True if contains_uppercase: # ๐๏ธ this runs print('The string contains uppercase letters') else: print('The string does NOT contain any uppercase letters')
any() function short-circuits and returns True.Alternatively, you can use a for loop.
This is a three-step process:
for loop to iterate over the string.str.isupper() method to check if each character is an uppercase
letter.True and break out of the loop.contains_uppercase = False my_str = 'Bobby' for char in my_str: if char.isupper(): contains_uppercase = True break print(contains_uppercase) # ๐๏ธ True

We used a for loop to iterate over the string.
On each iteration, we check if the current character is an uppercase letter.
contains_uppercase variable to True and break out of the for loop.The break statement breaks out of the
innermost enclosing for or while loop.
Alternatively, you can use the re.search() method.
The re.search() method will return a match object if the string contains
uppercase letters, otherwise, None is returned.
import re def contains_uppercase(string): return bool(re.search(r'[A-Z]', string)) if contains_uppercase('Bobby'): # ๐๏ธ this runs print('The string contains uppercase letters') else: print('The string does NOT contain any uppercase letters') print(contains_uppercase('A b c')) # ๐๏ธ True print(contains_uppercase('abc 123')) # ๐๏ธ False print(contains_uppercase('')) # ๐๏ธ False print(contains_uppercase(' ')) # ๐๏ธ False
The re.search() method looks for the first location in the string where the provided regular expression produces a match.
re.search() method finds a match, it will return a match object, otherwise None is returned.The first argument we passed to the re.search() method is a regular
expression.
The square brackets [] are used to indicate a set of characters.
A-Z characters represent a range of uppercase letters.We used the
bool()
class to convert the match object to True or the None value to False.
You can learn more about the related topics by checking out the following tutorials: