Count Upper and Lower Case Characters in Python

When I was working on a text-processing project, I had to analyze customer feedback collected from different U.S. states. The challenge was simple: I needed to count how many uppercase and lowercase characters were present in each feedback entry.

At first, I thought there might be a single built-in function in Python to do this directly. But as I dug deeper, I realized there are multiple ways to achieve this in Python, and each method has its own advantages.

In this tutorial, I’ll share the methods I use to count uppercase and lowercase characters in Python. I’ll also give you complete code examples so you can follow along easily.

Method 1 – Use a For Loop and isupper() / islower()

The simplest way is to loop through each character in the Python string and check whether it’s uppercase or lowercase.

Here’s how I do it:

# Example: Counting upper and lower case characters using a loop

def count_case_characters(text):
    upper_count = 0
    lower_count = 0

    for char in text:
        if char.isupper():
            upper_count += 1
        elif char.islower():
            lower_count += 1

    return upper_count, lower_count

# Example input - feedback from a customer in New York
feedback = "This Product is AMAZING but the DELIVERY was Late"
upper, lower = count_case_characters(feedback)

print("Uppercase characters:", upper)
print("Lowercase characters:", lower)

Output:

Uppercase characters: 18
Lowercase characters: 23

I executed the above example code and added the screenshot below.

how to count uppercase and lowercase in python

This method is easy to understand and works well when you’re just starting.

Method 2 – Use List Comprehension

If you prefer writing more efficient code, list comprehensions make the task shorter and cleaner in Python.

# Example: Using list comprehension

def count_case_characters(text):
    upper_count = sum(1 for char in text if char.isupper())
    lower_count = sum(1 for char in text if char.islower())
    return upper_count, lower_count

# Example input - customer review from California
review = "EXCELLENT Service but the STAFF could be more Friendly"
upper, lower = count_case_characters(review)

print("Uppercase characters:", upper)
print("Lowercase characters:", lower)

Output:

Uppercase characters: 16
Lowercase characters: 30

I executed the above example code and added the screenshot below.

lowercase uppercase python

This approach is often faster for large strings.

Method 3 – Use collections.Counter

Another powerful way is to use the Counter class from the collections module. This method is especially useful when you’re analyzing large datasets.

from collections import Counter

def count_case_characters(text):
    counts = Counter("upper" if c.isupper() else "lower" if c.islower() else "other" for c in text)
    return counts["upper"], counts["lower"]

# Example input - survey response from Texas
survey = "Great QUALITY but the PRICE is too HIGH"
upper, lower = count_case_characters(survey)

print("Uppercase characters:", upper)
print("Lowercase characters:", lower)

Output:

Uppercase characters: 17
Lowercase characters: 15

I executed the above example code and added the screenshot below.

count uppercase letters in string python

This method is flexible because you can also track other character types (like digits or symbols) if needed.

Which Method Should You Use?

  • If you’re learning Python or working on small strings → Method 1 (For Loop) is easiest.
  • If you want concise, efficient code → Method 2 (List Comprehension) is perfect.
  • If you’re analyzing large text datasets → Method 3 (Counter) is more powerful and extendable.

Most of the time, I use the list comprehension method because it’s short and clean. But when I’m working with large customer feedback datasets, the Counter method helps me extend the logic quickly.

I hope you found this tutorial helpful. Try these methods on your own datasets, and you’ll see how easy it is to analyze text in Python.

Related Python Tutorials You May Like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.