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 出阁宴祝福语姐姐简短