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.
3 odpowiedzi
+ 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)
0
Thanks!!