Check if a string contains any Uppercase letters in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a string contains any Uppercase letters in Python #

To check if a string contains any uppercase letters:

  1. Use a generator expression to iterate over the string.
  2. Use the str.isupper() method to check if each character is uppercase.
  3. If the condition is met for any of the characters, the string contains uppercase letters.
main.py
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.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

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.

main.py
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.

main.py
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')
If the condition is met for any of the characters in the string, the any() function short-circuits and returns True.

Alternatively, you can use a for loop.

Check if a string contains any Uppercase letters using a for loop #

To check if a string contains any uppercase letters:

  1. Use a for loop to iterate over the string.
  2. Use the str.isupper() method to check if each character is an uppercase letter.
  3. If the condition is met, set a variable to True and break out of the loop.
main.py
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.

If the condition is met, we set the 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.

Check if a string contains any Uppercase letters using re.search() #

Use the re.search() method to check if a string contains any uppercase letters, e.g. re.search(r'[A-Z]', string). The re.search() method will return a match object if the string contains uppercase letters, otherwise None is returned.

main.py
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.

If the 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.

The 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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.