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 爱生日祝福语简短独特