Censor Spaces


Hey guys I have a problem with the spaces:

def censor(text, word):
    cens = "*" * len(word)
    res = ''
    for i in range(len(text.split())):
        if text.split()[i] == word:
            res = res + cens + ' '
            res =  res + text.split()[i]
    return res
censor("hey hey hey","hey")

So this code is returning one space more at the end: "*** *** *** " instead of "*** *** ***" Any idea how I can omit the last space intelligently?


slice it off? Slices have been covered,right?, yea, they have, you used slices in pyglatin and more

remember slices? for example:

print "hello world"[0:5]

you could use negative values to start at the right hand side.


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