Extract the lowercase characters from a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 6, 2022

banner

Photo from Unsplash

Extract the lowercase characters from a String in Python #

To extract the lowercase characters from a string:

  1. Use a generator expression to iterate over the string.
  2. Use the str.islower() method to check if each value is a lowercase character.
  3. Use the str.join() method to join the matching characters into a string.
main.py
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 and extracted all lowercase characters.

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 value is a lowercase character.

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.

The last step is to use the str.join() method to join the matching characters into a string.

main.py
my_str = 'bobbyHADZ123' only_lower = ''.join(char for char in my_str if char.islower()) print(only_lower) # 👉️ bobby

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

Here is an example that joins the characters with a space separator.

main.py
my_str = 'bobbyHADZ123' only_lower = ' '.join(char for char in my_str if char.islower()) print(only_lower) # 👉️ b o b b y

An alternative approach is to use the ascii_lowercase attribute from the string built-in module.

main.py
import string print(string.ascii_lowercase) # 👉️ abcdefghijklmnopqrstuvwxyz print(string.ascii_uppercase) # 👉️ ABCDEFGHIJKLMNOPQRSTUVWXYZ my_str = 'bobbyHADZ123' only_lower = ''.join(char for char in my_str if char in string.ascii_lowercase) print(only_lower) # 👉️ bobby

We used the string.ascii_lowercase attribute to get a string containing the letters from a to z.

On each iteration, we use the in operator to check if the current letter is one of the lowercase letters and return the result.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

The last step is to use the str.join() method to join the matching letters.

If you need to extract the lowercase characters from a string into a list, use a list comprehension.

main.py
my_str = 'bobbyHADZ123' only_lower = [char for char in my_str if char.islower()] print(only_lower) # 👉️ ['b', 'o', 'b', 'b', 'y']

On each iteration, we check if the character is a lowercase letter and return the result.

The new list only contains the lowercase characters from the string.

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.