Count Number of Lowercase Characters in a String in Python Program

PythonServer Side ProgrammingProgramming

When it is required to count the number of lower case characters in a string, the ‘islower’ method and a simple ‘for’ loop can be used.

Below is the demonstration of the same −

Example

 Live Demo

my_string = "Hi there how are you"
print("The string is ")
print(my_string)
my_counter=0

for i in my_string:
   if(i.islower()):
      my_counter=my_counter+1
print("The number of lowercase characters in the string are :")
print(my_counter)

Output

The string is
Hi there how are you
The number of lowercase characters in the string are :
15

Explanation

  • A string is defined and is displayed on the console.

  • A counter value is initialized to 0.

  • The string is iterated over, and checked to see if it contains lower case alphabets using the ‘islower’ method.

  • If so, the counter is incremented by 1 until the end of the string.

  • This is displayed as output on the console.

raja
Published on 17-Apr-2021 12:06:34
Advertisements