Function programme to count number of uppercase letters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Function programme to count number of uppercase letters

This is the code i've written s=str(input("String:")) def str_info(a): c=0 l=a.split() for i in l: if i.isupper()==True: c+=1 print("No. of uppercase:",c) str_info(s) In the output though, the value of c isn't updating so the value of uppercase letters remains 0 What modifications should i make in the code? Please help out.

21st Jun 2020, 12:59 PM
Sriya
2 Answers
+ 7
The issue is that you split the input string to a list. So if input is: 'Hello World', the resulting list is ['Hello', 'World']. If you now check for isupper() in the for loop for the first element *Hello*, the result is False. Only if the complete String is in upper case the result will be True. You can use the input string as it is, and you can iterate throuhg it character by character. BTW, you dont need to convert input to string at input(). input() always return a string. s=input("String:") def str_info(a): c=0 #l=a.split() for i in s: if i.isupper()==True: c+=1 print("No. of uppercase:",c) str_info(s)
21st Jun 2020, 2:16 PM
Lothar
Lothar - avatar
0
Thanks!!
21st Jun 2020, 3:27 PM
Sriya