Computer >> Computer tutorials >  >> Programming >> Python

Python program to count upper and lower case characters in a given string.


For a given string input, we'd like to count the number of characters in lower case and those in the upper case using python. For example, for the given string,

"Hello World"

The counts should be −

Upper case: 2
Lower case: 8

We can implement this using a simple for loop with 2 conditions to check for upper and lower case characters. For example,

Example

def countUpperAndLowerCase(sentence):
upper = 0
lower = 0
for i in sentence:
if i >='A' and i <= 'Z':
upper += 1
elif i >= 'a' and i <= 'z':
lower += 1
print("Upper case: " + str(upper))
print("Lower case: " + str(lower))

countUpperAndLowerCase("Hello World")

Output

This will give the output −

Upper case: 2
Lower case: 8