Find and Replace a Word

Easy ⏱ 8 min 85% acceptance ★★★★☆ 4.3
Write replace_word(s, old, new) that returns s with every exact, case-sensitive occurrence of old replaced by new.

Examples

Example 1
Input
s = 'I like cats. Cats are great.', old = 'Cats', new = 'Dogs'
Output
'I like cats. Dogs are great.'
Explanation

Only the capitalized 'Cats' matches; lowercase 'cats' is untouched.

Constraints

  • old is non-empty.

Topics

Stringsreplace

Companies

SwiggyUber

Hints

Hint 1

`str.replace(old, new)` replaces all occurrences by default.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.