Hey, Please any help, I'm getting this error message each time.

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

Thanks in advance

How can I remove the last whitespace? I tried to use strip(), but it doesn't work!

def censor(text,word):
    cen = []
    for item in text.split():
        if item == word:
            cen += "*" * len(word) + " "
            cen += item + " "
    return "".join(cen)


strip() should the trick, you can also slice it off


delete your last line, and instead add:

temp_cen = "".join(cen) 
n = len(temp_cen) - 1
real_cen = temp_cen[0:n]
print real_cen
return real_cen

How it works :

# create a string where you store the result of your joined lists.
temp_cen = "".join(cen)

# calculate and store the length of that string, minus 1. Because that last one is the " " that is too many at the end.
n = len(temp_cen) - 1

# create the final result, a string that takes the output of the joined strings but drops the last character " "
real_cen = temp_cen[0:n]
print real_cen
return real_cen


you can use negative values to slice off on the right hand side:

return "".join(cen)[:-1]

or using strip:

return "".join(cen).strip()

bit simpler, and less code (or do this in two lines, that is also possible)


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