10 Censory



My code manages to censor the correct words with asterisks but for some reason I can't seem to get the spaces correct.

When I run this code I get the following error:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "******" when it should return " *** ***".

def censor(text,word):
    n_list = ''
    for i in text.split():
        if i == word:
            n_list+='*' * len(word) 
            ' '.join(n_list)
        if i != word:
            ' '.join(n_list)
    return n_list

I tried putting adding a space after a word is replaced by an asterisk by changing the line to : 
n_list+='*' * len(word) + ' '

This however leaves me with an extra space once the last word has been replaced.


string.join adds the delimiter between each occurrence, so if that's not what you use as the result then what do you use? And if you don't use the result of string.join, then what is it doing there?


Consider adding each i != word to n_list in the same way as in i == word, including the extra character. Then before you return your list, create a new string (m_list) as:

m_list = n_list[0:len(n_list)-1]

and return m_list.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.