Python: Count Uppercase Characters in a String

A Python string is a collection of characters surrounded by single, double, or triple quotes. The computer does not understand the characters; instead, it stores the manipulated character as a combination of 0’s and 1’s internally.

In this article we are going to count the uppercase letters in the given string

Examples:

Input:

string = "BTechGeeks"

Output:

The given string contains 3 uppercase letters

Count Uppercase Characters in a String

There are several ways to count uppercase letters in the given string some of them are:

Method #1:Using sum() + isupper() function

Using a generator expression, we can iterate over all of the characters in a string. If an uppercase character is discovered during iteration, pass it to the sum() function. Finally, the sum() function returns the total number of uppercase characters in a string.

Below is the implementation:

# given string
string = "BTechGeeks"
# counting uppercase letters in the given string
upper_count = sum(1 for element in string if element.isupper())
# print the count of uppercase letters
print("The given string contains", upper_count, "uppercase letters")

Output:

The given string contains 3 uppercase letters

Method #2:Using for loop

Using a for loop, we can iterate over a string character by character, counting the number of uppercase characters in the string as we go.

Below is the implementation:

# given string
string = "BTechGeeks"
# counting uppercase letters in the given string
upper_count = 0
# traverse the string using for loop
for element in string:
    # checking if the character is in uppercase
    if element.isupper():
      # increase the count if the given letter is upper
        upper_count += 1
# print the count of uppercase letters
print("The given string contains", upper_count, "uppercase letters")

Output:

The given string contains 3 uppercase letters

Method #3:Using List Comprehension

Iterate over all the characters in a string using list comprehension to create a list of only uppercase characters from the string. Then, by obtaining the size of that uppercase character list, we can obtain the number of uppercase characters in the string.

Below is the implementation:

# given string
string = "BTechGeeks"
# counting uppercase letters in the given string using list comprehension
upper_count = len([element for element in string if element.isupper()])
# print the count of uppercase letters
print("The given string contains", upper_count, "uppercase letters")

Output:

The given string contains 3 uppercase letters

Method #4:Using regex

With a pattern that matches all uppercase characters in the string, we can use Python’s regex module’s findall() method. findall() returns a list of all matches in the string, which in our case will be upper case characters. Then, by retrieving the size of that uppercase character list, we can calculate the number of uppercase characters in the string.

Below is the implementation:

import re
# given string
string = "BTechGeeks"
# counting uppercase letters in the given string using regex
upper_count = len(re.findall(r'[A-Z]', string))
# print the count of uppercase letters
print("The given string contains", upper_count, "uppercase letters")

Output:

The given string contains 3 uppercase letters

Related Programs:

Leave a Comment