Error: extra space in the end


#1

I keep getting an error that I have an extra space in the end even though I thought I eliminated it by using count. Could somebody help?


10. Censor
#2

did you try to remove this space ??


#4

I thought I needed the space because if it's not the end of the sentence the space should be there. I actually reworked my code:

   def censor(text,word):
        new_text=""
        items=text.split()
        count=len(items)-1
        for i in range(len(items)):
            if items[i]==word:
                new_word="*"*len(word)
                new_text=new_text+new_word
                if i<count:
                    new_text+=" "
            else:
                new_text=new_text+items[i]
                if i<count:
                    new_text+=" "
        return new_text