Tuesday, 21 September 2021

Python: Count number of lowercase characters in a string

By iterating over the string character by character, we can count number of lower case characters in a string.

 

count_lowercase_chars.py

def count_lowercase_characters(input_str):
    count = 0

    for ch in input_str:
        if ch.islower():
            count += 1

    return count

input = 'Hello WorlD'

print('Number of lowercase characters in "',input,'" is ', count_lowercase_characters(input))

 

Output

Number of lowercase characters in " Hello WorlD " is  7

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment