Search⌘ K
AI Features

Solution: Calculate the Number of Uppercase and Lowercase Letters

Explore how to write a Python function that counts uppercase and lowercase letters in a string. Understand how to use dictionaries, loops, and conditional checks to solve this common string manipulation problem efficiently.

We'll cover the following...

The solution to the problem of calculating the number of uppercase and lowercase letters in Python is given below.

Solution

Python 3.8
def count_lower_upper(s) :
dlu = {'Lower' : 0, 'Upper' : 0}
for ch in s :
if ch.islower( ) :
dlu['Lower'] += 1
elif ch.isupper( ) :
dlu['Upper'] += 1
return(dlu)
d = count_lower_upper('James BOnd ')
print(d)

Explanation

  • Line 2:
...