split 能把字符串分割成列表,join是把一些东西加进字符串里面
def censor(text, word):
words = text.split(" ")
print(words)
w = []
for i in words:
if i == word:
w.append(len(i)*"*")
else:
w.append(i)
print(w)
print (" ".join(w))
a = " ".join(w)
print(type(a))
return " ".join(w)
censor("this hack is wack hack", "hack")
输出:
['this', 'hack', 'is', 'wack', 'hack']
['this', '****', 'is', 'wack', '****']
this **** is wack ****
<class 'str'>
网友评论