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

PythonProgrammingServer Side Programming

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,


 Live Demo

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")


This will give the output −

Upper case: 2
Lower case: 8
Updated on 20-Jun-2020 08:59:27