Count the uppercase letters in a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Count the uppercase letters in a String in Python #

To count the uppercase letters in a string:

  1. Use a generator expression to iterate over the string.
  2. Use the str.isupper() method to check if each character is uppercase.
  3. Use the sum() function to get the count of the uppercase letters.
main.py
my_str = 'BOBBYhadz123' # ✅ count uppercase letters in string uppercase_count = sum(1 for char in my_str if char.isupper()) print(uppercase_count) # 👉️ 5 # ------------------------------------------------- # ✅ extract uppercase letters from string only_upper = ''.join(char for char in my_str if char.isupper()) print(only_upper) # 👉️ BOBBY

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 uppercase and return 1 if it is.

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' # ✅ check if all characters in a string are uppercase all_uppercase = my_str.isupper() print(all_uppercase) # 👉️ True

The last step is to use the sum() function to get the total count of the uppercase letters in the string.

main.py
my_str = 'BOBBYhadz123' uppercase_count = sum(1 for char in my_str if char.isupper()) print(uppercase_count) # 👉️ 5

The sum function takes an iterable, sums its items from left to right and returns the total.

You can use the same approach if you need to extract the uppercase letters from the 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']

The first example uses the str.join() method to join the uppercase characters from the string into a new string.

The second example returns a list containing the uppercase characters from the string.

Alternatively, you can use a simple for loop.

Count the uppercase letters in a String using a for loop #

To count the uppercase letters in a string:

  1. Use a for loop to iterate over the string.
  2. Use the str.isupper() method to check if each character is uppercase.
  3. If the condition is met, increment a count variable by 1.
main.py
my_str = 'BOBBYhadz123' uppercase_count = 0 for char in my_str: if char.isupper(): uppercase_count += 1 print(uppercase_count) # 👉️ 5

We used a for loop to iterate over the string.

If the current character is an uppercase letter, we increment the value stored in the uppercase_count variable by 1.

The += operator is a shorthand for uppercase_count = uppercase_count + 1.

The following code sample achieves the same result.

main.py
my_str = 'BOBBYhadz123' uppercase_count = 0 for char in my_str: if char.isupper(): uppercase_count = uppercase_count + 1 print(uppercase_count) # 👉️ 5
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.