Python Program to Count Uppercase and Lowercase Letters
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
How to Think About It
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
Code
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}')
Dry Run
Let's trace the input 'Hello World' through the code
Input string
text = 'Hello World'
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
Count lowercase letters
Lowercase count = 8
Print result
Output: 'Uppercase letters: 2, Lowercase letters: 8'
| Character | Is Uppercase? | Is Lowercase? |
|---|---|---|
| H | Yes | No |
| e | No | Yes |
| l | No | Yes |
| l | No | Yes |
| o | No | Yes |
| No | No | |
| W | Yes | No |
| o | No | Yes |
| r | No | Yes |
| l | No | Yes |
| d | No | Yes |
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
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}')
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}')
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Generator expressions with sum | O(n) | O(1) | Concise and fast for simple counts |
| For loop with counters | O(n) | O(1) | Clear and easy to understand for beginners |
| collections.Counter filtering | O(n) | O(n) | Counting multiple character types efficiently |
isupper() and islower() methods to easily check letter cases.