Python-courses-python-exercises-python-for-while

Exercise 64

Write a Python program to count the Uppercase characters within a given string. Example if s = "Python Programming", the program must returns 2.

Solution

def countUpper(s):

# initializing the number of uppercase characters
numberUpper = 0

# iterate over all characters of s:
for x in s:
if x.isupper():
numberUpper = numberUpper + 1

return numberUpper

#Testing algorithm:
s = "Python Programmin"
print("The number of uppercase characters in s is :" , countUpper(s))
# The output is :
# The number of uppercase characters in s is : 2

Younes Derfoufi
my-courses.net

Leave a Reply