代码示例:
def replace_sentence(original_text, old_sentence, new_sentence):
"""
Replaces the old sentence with the new sentence in the original text.
"""
return original_text.replace(old_sentence, new_sentence)
示例调用:
original_text = "The quick brown fox jumps over the lazy dog."
old_sentence = "brown fox"
new_sentence = "red fox"
new_text = replace_sentence(original_text, old_sentence, new_sentence)
print(new_text)
# Output: The quick red fox jumps over the lazy dog.
将函数改写为接受三个参数,分别是原始文本、需要替换的句子和新的句子。然后在函数中使用replace()函数替换句子,并返回新的文本。以上代码示例展示了如何使用新的函数在原始文本中将“brown fox”替换为“red fox”。