如果要用Python实现这样一个函数:在一个字符串text 中查找 word ,并替换成 “*”
For example:
censor("this hack is wack hack", "hack")
should return
"this **** is wack ****"
只需要一行就可以搞定:
def censor(test,word):
return (len(word) * "*").join(test.split(word))
验证效果:
输入
print censor("hello,neo!","neo")
输出
hello,***!
非常方便!