Error that shouldn't occur in the censor task


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

This code works, but the error says it doesn't. What's wrong?


def censor(text,word):
    TextSplit = text.split()
    EndList = []
    Result = ''
    for TextWord in TextSplit:
        if TextWord == word:
            EndList.append("*" * len(word))
        else:
            EndList.append(TextWord)
    for Component in EndList:
        Result += Component
        Result += ' '
    return Result


#2

Wait, I get it, it's adding an extra ' ' at the end. How do I do it then, with a list of correct components? I can't find anything that helps.


#3

you could just use list slice? for example:

print "list slicing"[:-1]

will slice of the last character, you could do this just before your return. Or, you could use .join() to join the list into a string, this way you don't have to deal with additional space at the end


#4

Thanks! I was trying with more things to check whether the component being added was the last one in the list, but your solution worked best


#5

checking if it is the last one will also work, but slicing is easier and .join() would be the easiest, that is even easier. But like we said, there are multiply ways to solve a problem