Bug: Python: Practice Makes Perfect: Censor



If you use re, you can filter out parts of other words and the code still passes

If the operator decides to use re, the code should only pass if only the word as a whole is filtered out
Code below returns this: th** ** a sample sentence to censor
Change which 'res' line is commented out, returns this: this ** a sample sentence to censor

import re

def censor(text,word):
    length = len(word)
    #res = re.sub(r"\b%s\b" % word,"*"*length,text)
    res = re.sub(word,"*"*length,text)
    return res

print(censor("this is a sample sentence to censor","is"))


unfortunately there are no such test cases, so its allowed. same with .replace()


Ok, thanks for getting back to me :thumbsup:


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