Extract the uppercase characters from a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 6, 2022

banner

Photo from Unsplash

Extract the uppercase characters from a String in Python #

To extract the uppercase characters from a string:

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

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.

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

main.py
my_str = 'BOBBYhadz123' only_upper = ''.join(char for char in my_str if char.isupper()) print(only_upper) # 👉️ 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_upper = ' '.join(char for char in my_str if char.isupper()) print(only_upper) # 👉️ B O B B Y

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

main.py
import string print(string.ascii_uppercase) # 👉️ ABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_lowercase) # 👉️ abcdefghijklmnopqrstuvwxyz my_str = 'BOBBYhadz123' only_upper = ''.join(char for char in my_str if char in string.ascii_uppercase) print(only_upper) # 👉️ BOBBY

We used the string.ascii_uppercase attribute to get a string containing the uppercase letters from A to Z.

On each iteration, we use the in operator to check if the current letter is one of the uppercase 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 uppercase characters from a string into a list, use a list comprehension.

main.py
my_str = 'BOBBYhadz123' only_upper = [char for char in my_str if char.isupper()] print(only_upper) # 👉️ ['B', 'O', 'B', 'B', 'Y']

On each iteration, we check if the character is an uppercase letter and return the result.

The new list only contains the uppercase 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.