Check if a string contains any Uppercase letters in Python

avatar
Borislav Hadzhiev

Last updated: Jan 25, 2023
3 min

banner

# Table of Contents

  1. Check if a string contains any Uppercase letters in Python
  2. Check if a string contains any Uppercase letters using a for loop
  3. Check if a string contains any Uppercase letters using re.search()

# 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']

check if string contains any uppercase letters

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

This is a three-step process:

  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

check if string contains any uppercase letters using for loop

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.

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

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.

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev