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 −
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)
The string is Hi there how are you The number of lowercase characters in the string are : 15
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.