Censor


#1

Trying to understand what I'm doing wrong. My code removes all the spaces when creating the new_text. Replaces the selected word with the '*', but prints everything together.

Sorry if this is a bad question. I'm new and trying to grasp the basics. I've been stuck on this one a couple of days.


def censor(text, word):
    text = text.split()
    new_text = []
    for i in text:
        if i == word:
            y = len(i) * '*'
            new_text += y
                        
        else:
            new_text += i

    return ''.join(new_text)

    
print censor('hey hey hey', 'hey')


#2

here:

return ''.join(new_text)

what is between the apostrophes, is what is used for joining, so:

'-'.join()

would be a hyphen between every word.

also, you might want to read this


#3

Thank you. I'll read up on the link.


#4

if you need more help, post an updated version of your code


#5

I figured it out. thank you.

def censor(text, word):
    new_text = []
    text = text.split()
    for i in text:
        if i == word:
            x = len(word)*'*'
            y = i.replace(word, x)
            new_text.append(y)
            
        else:
            new_text.append(i) 

    return ' '.join(new_text)
    
print censor('hey hey hey', 'hey')

#6