TGViewer
About Python [ru] About Python [ru] @python_tesst · 6.42K subscribers
Post #1448 1.04K
👩‍💻 Задачка по Python

Напишите функцию, которая принимает строку и два дополнительных аргумента — слово для замены и новое слово. Функция должна заменить все вхождения указанного слова в строке на новое слово.

➡️ Пример:

Входная строка:
"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."
More from @python_tesst
  1. Oct 10, 2026Python 3.15 на проде: ленивые импорты, frozendict, UTF-8 из коробки и JIT, который наконец…
  2. Oct 9, 2026Haiku 5.5 — коротко по делу В среднем один запуск выходит на 75% дешевле, чем у Haiku 4.5.…
  3. Oct 8, 202650 промптов для ChatGPT, после которых толстенный PDF или статья вовсе не обязательно чита…
  4. Oct 8, 2026Сидишь такой, готовишь стартап на 500M$+, а лимиты сдохли уже на втором запросе. 👉 About…
  5. Oct 7, 2026Python в Cloudflare Workers: всё, теперь по-взрослому — GA Cloudflare перестала держать Py…
  6. Oct 7, 2026PyCoder’s Weekly #755 — 6 октября 2026 Python 3.15.0rc3: в новых фичах lazy loading что-то…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →