Censor spacing error


Link to exercise

I don't understand how to get rid of space at the end of the sentence.

Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".

I need to find a simple way to get the spacing ONLY between the words.

def censor(text, word):
    t_Cen = ""
    w_Cen = '*' * len(word)
    wlist = text.split()
    for w in wlist:
        if w == word:
            t_Cen += (w_Cen + ' ')
        elif w!= wlist:
            t_Cen += (w + ' ')
    return t_Cen.


This isn't an answer but if you get stuck for a long time and want to move on, just use this:

import re
def censor(text, word):
for i in text:
string = re.sub(word, '*' * len(word), text)
return string

(Don't forget to indent where needed)


Thank you very much!


I'm not sure how to fix it in your code exactly but what I did was make an empty list instead of an empty string and appended my items then returned the list with " ".join(). So not much different but it doesn't worry about adding spaces when iterating


I was having the same issue, and what I decided to do was only return the final phrase but ommit the last character in the string, like so:

def censor(text,word):
    length = len(word)
    newlist = text.split()
    phrase = ""
    for term in newlist:
        if term == word:
            term = "*" * length
            phrase += term + " "
            phrase += term + " "
    return phrase[:-1]

Now that I solved it, I came here to see what others did cause it just feels like such a bad solution what I did :slight_smile:


@nicolai9 can you show your code? Sounds interesting.



romanzs solution is nice, the problem with jcervantesla and alt_silver solution is that string manipulation is extensive. because you can't manipulate a string, what in essence is happening here:

phrase += term + " "

is that the string stored in phrase is copied, and term is added. Now, for a small program like this, it doesn't really matter, but if optimization is a issue, and the amount of data is bigger, such things become an issue.

@jcervantesla one optimizations for your code, this lines:

length = len(word)

i would personally do:

term = len(word) * "*"

and then inside the if condition:

if term == word:
   phrase += term + " "

saves a line of code, and you don't have to calculate the asterisks in every run of the loop, you only do it once.

i would still recommend to use lists instead, those can be manipulated, saving memory. lists don't have to make a copy (like strings)

one more thing, python has a built in replace function:


this should allow for a single line solution, just in case you where curious


Thanks, I appreciate the analysis, tips and resources! @stetim94


def censor(text, word):
    foo = []
    newL = text.split()
    wordL = len(word)
    for i in newL:
        if i == word:
            wor =  "*" * wordL
    return " ".join(foo)


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