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 红包祝福语简短10字
18 周六早晨祝福语简短