I just started to learn python a few months ago so I'm a newbie here. I was trying to capitalize the first letter of every word in a string. When the input is "hello world" (for example) it works perfectly, but with some inputs like "i love coding" it returns this "I Love CodIng" and it just doesn't make sense to me. Could someone explain to me why this is happening? Here's the code:
def LetterCapitalize(str):
str = str.replace(str[0], str[0].upper())
for i in range(len(str)):
try:
if str[i] == ' ':
str = str.replace(str[i+1], str[i+1].upper())
else:
continue
except IndexError:
break
return str