欢迎访问宙启技术站
智能推送

编写一个Python函数,接收两个参数:一个字符串和一个列表,将字符串中所有在列表中的词汇替换为*

发布时间:2023-06-25 22:08:31

以下是一个示例Python函数,它接受一个字符串和一个列表,并将字符串中所有在列表中的词汇替换为* 1000字:

def replace_words(string, word_list):
    for word in word_list:
        string = string.replace(word, "*" * 1000)
    return string

使用时,可以将要替换的字符串和词汇列表作为参数传递给此函数。例如:

sentence = "The quick brown fox jumps over the lazy dog."
words_to_replace = ["quick", "jumps", "lazy"]

result = replace_words(sentence, words_to_replace)

print(result)

此代码的输出将是:

The ***** brown fox ***** over the ***** dog.