Censor. Help me figure out this code


#1


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

What does split() do? How do I use it in this code. When the code runs it brings out this result":****Are****you****goddamn****crazy?" which is wrong since the asterisks are not in right place

How do I solve this situation? Thanks

def censor(text,word):
    text=text.split()
    result=[]
    for w in text:
        result.append("*"*len(word))
        result.append(w)
    return "".join(result)
        
print censor("Are you goddamn crazy?","damn")


#2

you need an if condition to check if w equals word, in that case append asterix's, else append the word (stored in the w variable)


#3

Hi, so does else: return.append(w) come after return "'.join(result)?


#4

no, and no. you shouldn't append to return. .join() will join the entire list into a string, so it is the last thing the function does


#5

This is my new code and the asteriks won't be printed anymore. What did I miss?

def censor(text,word):
    text=text.split()
    result=[]
    for w in text:
        if w==word:
            result.append("*"*len(word))
        else:
            result.append(w)
    return "".join(result)   
print censor("Are you goddamn crazy?","damn")

#6

split() will split a string into a list, using a separator (default: space)

you should use " ".join() with a space, so the words are separated with a space

there is nothing to censor in your example, goddamn doesn't equal damn, so you would need to make damn a separate word


#7

Thank you. I think now I now what split() does.


#9