Exercise 64
Write a Python program as function which takes a string 's' as parameter and return the number of Uppercase characters within the string 's'. Example: if s = "Python Programming", the function must returns 2.
Solution
def count_uppercase(s):
# We initialize a counter variable count to 0
count = 0
# and then loop through each character in the string s
for char in s:
# we test if the character is uppercase
# and the we increment the count variable by 1
if char.isupper():
count += 1
return count
s = "Python Programming"
print(count_uppercase(s)) # Output: 2
def count_uppercase(s):
# We initialize a counter variable count to 0
count = 0
# and then loop through each character in the string s
for char in s:
# we test if the character is uppercase
# and the we increment the count variable by 1
if char.isupper():
count += 1
return count
s = "Python Programming"
print(count_uppercase(s)) # Output: 2
def count_uppercase(s): # We initialize a counter variable count to 0 count = 0 # and then loop through each character in the string s for char in s: # we test if the character is uppercase # and the we increment the count variable by 1 if char.isupper(): count += 1 return count s = "Python Programming" print(count_uppercase(s)) # Output: 2
Younes Derfoufi
my-courses.net
[…] Exercise 64 || Solution […]