0
0
PythonProgramBeginner · 2 min read

Python Program to Count Uppercase and Lowercase Letters

Use a loop to check each character with char.isupper() and char.islower() and count them; for example, uppercase = sum(1 for c in text if c.isupper()) and lowercase = sum(1 for c in text if c.islower()).
📋

Examples

InputHello World
OutputUppercase letters: 2, Lowercase letters: 8
InputPYTHON programming
OutputUppercase letters: 6, Lowercase letters: 11
Input1234!@#
OutputUppercase letters: 0, Lowercase letters: 0
🧠

How to Think About It

To count uppercase and lowercase letters, look at each character in the string one by one. Check if it is uppercase using isupper() or lowercase using islower(). Keep two counters and add one each time you find a matching letter. Ignore characters that are not letters.
📐

Algorithm

1
Get the input string from the user.
2
Initialize two counters: one for uppercase letters and one for lowercase letters.
3
For each character in the string, check if it is uppercase or lowercase.
4
If uppercase, increase the uppercase counter by one.
5
If lowercase, increase the lowercase counter by one.
6
After checking all characters, print the counts.
💻

Code

python
text = input('Enter a string: ')
uppercase = sum(1 for c in text if c.isupper())
lowercase = sum(1 for c in text if c.islower())
print(f'Uppercase letters: {uppercase}, Lowercase letters: {lowercase}')
Output
Enter a string: Hello World Uppercase letters: 2, Lowercase letters: 8
🔍

Dry Run

Let's trace the input 'Hello World' through the code

1

Input string

text = 'Hello World'

2

Count uppercase letters

Check each character: 'H'(upper), 'e'(lower), 'l'(lower), 'l'(lower), 'o'(lower), ' '(space), 'W'(upper), 'o'(lower), 'r'(lower), 'l'(lower), 'd'(lower) Uppercase count = 2

3

Count lowercase letters

Lowercase count = 8

4

Print result

Output: 'Uppercase letters: 2, Lowercase letters: 8'

CharacterIs Uppercase?Is Lowercase?
HYesNo
eNoYes
lNoYes
lNoYes
oNoYes
NoNo
WYesNo
oNoYes
rNoYes
lNoYes
dNoYes
💡

Why This Works

Step 1: Check each character

The program looks at every character in the string one by one to decide if it is uppercase or lowercase.

Step 2: Use built-in methods

It uses isupper() to check uppercase and islower() to check lowercase letters, which are simple and reliable.

Step 3: Count and print

It counts how many times each condition is true and then prints the totals for uppercase and lowercase letters.

🔄

Alternative Approaches

Using a for loop with counters
python
text = input('Enter a string: ')
uppercase = 0
lowercase = 0
for c in text:
    if c.isupper():
        uppercase += 1
    elif c.islower():
        lowercase += 1
print(f'Uppercase letters: {uppercase}, Lowercase letters: {lowercase}')
This method is more explicit and easier to understand for beginners but uses more lines.
Using collections.Counter with filtering
python
from collections import Counter
text = input('Enter a string: ')
counter = Counter(text)
uppercase = sum(count for char, count in counter.items() if char.isupper())
lowercase = sum(count for char, count in counter.items() if char.islower())
print(f'Uppercase letters: {uppercase}, Lowercase letters: {lowercase}')
This method counts all characters first, then filters uppercase and lowercase, which can be efficient for large texts.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each character once, so the time grows linearly with the string length, making it O(n).

Space Complexity

It uses only a few counters and no extra space proportional to input size, so space complexity is O(1).

Which Approach is Fastest?

Using generator expressions with sum is concise and fast; explicit loops are clear but slightly longer; using Counter adds overhead but can be useful for multiple counts.

ApproachTimeSpaceBest For
Generator expressions with sumO(n)O(1)Concise and fast for simple counts
For loop with countersO(n)O(1)Clear and easy to understand for beginners
collections.Counter filteringO(n)O(n)Counting multiple character types efficiently
💡
Use isupper() and islower() methods to easily check letter cases.
⚠️
Counting characters without checking if they are letters can lead to wrong counts including spaces or symbols.