Python程序,用句子中的星号替换单词
我们可以使用程序在句子中用星号替换单词,以检查句子中的脏话等。例如,
如果我们有一个句子,
"Go feed all the ducks in the lake"
我们要用星号代替的单词,比如鸭子。然后我们的最后一句话看起来像-
"Go feed all the ***** in the lake"
我们可以直接使用python中的replace函数来实现此功能。例如,
示例
def replaceWithAsterisk(sent, word): # Use the replace function to find and replace the word with # same number of asterisks as the length of the word. return sent.replace(word, "*" * len(word)) sent = "Go feed all the ducks in the lake" censored_sent = replaceWithAsterisk(sent, "ducks") print(censored_sent)
输出结果
这将给出输出-
Go feed all the ***** in the lake
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短