Напишите функцию, которая принимает строку и два дополнительных аргумента — слово для замены и новое слово. Функция должна заменить все вхождения указанного слова в строке на новое слово.
Входная строка:
"The weather is nice today. Nice weather makes me happy."
Слово для замены: "nice"
Новое слово: "great"
Ожидаемый вывод:
"The weather is great today. Great weather makes me happy."
Решение задачи
def replace_word(text, old_word, new_word):
return text.replace(old_word, new_word)
# Пример использования:
input_text = "The weather is nice today. Nice weather makes me happy."
result = replace_word(input_text, "nice", "great")
print(result) # Ожидаемый результат: "The weather is great today. Great weather makes me happy."
Please open Telegram to view this post
VIEW IN TELEGRAM