Tuesday 21 September 2021

Python: Count number of uppercase characters in a string

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

 

count_uppercase_chars.py

def count_uppercase_characters(input_str):
    count = 0

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

    return count

input = 'Hello WorlD'

print('Number of uppercase characters in "',input,'" is ', count_uppercase_characters(input))

 

Output

Number of uppercase characters in " Hello WorlD " is  3

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment