Check if a string contains any Lowercase letters in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a string contains any Lowercase letters in Python #

To check if a string contains any lowercase letters:

  1. Use a generator expression to iterate over the string.
  2. Use the str.islower() method to check if each character is lowercase.
  3. If the condition is met for any of the characters, the string contains lowercase letters.
main.py
my_str = 'bobby HADZ' # ✅ Check if string contains lowercase letters contains_lowercase = any(char.islower() for char in my_str) print(contains_lowercase) # 👉️ True if contains_lowercase: # 👇️ this runs print('The string contains lowercase letters') else: print('The string does NOT contain lowercase letters') # -------------------------------------------------- # ✅ Extract the lowercase letters from the string my_str = 'bobbyHADZ123' only_lower = ''.join(char for char in my_str if char.islower()) print(only_lower) # 👉️ bobby only_lower = [char for char in my_str if char.islower()] print(only_lower) # 👉️ ['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.islower() method to check if the current character is a lowercase letter.

The str.islower method returns True if all cased characters in the string are lowercase and the string contains at least one cased character, otherwise False is returned.

main.py
my_str = 'bobbyhadz.com' all_lowercase = my_str.islower() print(all_lowercase) # 👉️ 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 HADZ' # ✅ Check if string contains lowercase characters contains_lowercase = any(char.islower() for char in my_str) print(contains_lowercase) # 👉️ True if contains_lowercase: # 👇️ this runs print('The string contains lowercase letters') else: print('The string does NOT contain lowercase 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 Lowercase letters using a for loop #

To check if a string contains any lowercase letters:

  1. Use a for loop to iterate over the string.
  2. Use the str.islower() method to check if each character is a lowercase letter.
  3. If the condition is met, set a variable to True and break out of the loop.
main.py
my_str = 'bobby HADZ' contains_lowercase = False for char in my_str: if char.islower(): contains_lowercase = True break print(contains_lowercase) # 👉️ True

We used a for loop to iterate over the string.

On each iteration, we check if the current character is a lowercase letter.

If the condition is met, we set the contains_lowercase 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 Lowercase letters using re.search() #

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

main.py
import re def contains_lowercase(string): return bool(re.search(r'[a-z]', string)) if contains_lowercase('Bobby'): # 👇️ this runs print('The string contains lowercase letters') else: print('The string does NOT contain any lowercase letters') print(contains_lowercase('A b c')) # 👉️ True print(contains_lowercase('ABC 123')) # 👉️ False print(contains_lowercase('')) # 👉️ False print(contains_lowercase(' ')) # 👉️ 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 lowercase 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.