sorry if this is a duplicate, but I haven't found any clear answer out there.
Basically, my program should capitalize a string like this :
'some words'
Into something like this :
'Some Words'
Here is my code:
mystring = "some words"
copy_of_mystring = ""
word = ""
for i in mystring:
if i==" ":
for j in range(1):
#capitalize if my word has more than 1 letter
if len(word) > 1:
word=str.upper(word[0]) + word[1:]
#capitalize if word has only 1 letter
elif len(word)==1:
word=str.upper(word[0])
copy_of_mystring += word + " "
word = ""
else:
word += i
print(copy_of_mystring)
So basically my first loop add i
to word
until i
is a space.
Then my second loop will start and capitalize word
and add it to copy_of_mystring
, that part is working.
And this is when my problem comes in. When my first loop is "building" my last word, it will of course won't find any space after it, so my problem comes from this condition :
if i==" ":
I tried this :
if i==" " or i==mystring[-1]:
But, in a string where several characters is the same as the last one, like in 'morgana'
, for example, it would end up like this
'Morg N'
So, is there a way to check if my character i
is the VERY LAST one in mystring
?
P.S: Sorry if this sounds like a really basic mistake but I'm still learning all the things we can do with strings.