Here's the Python code that uses the replace()
method to replace the word "dog" with "cat" in the given sentence:
main.py124 chars4 lines
Output:
main.py45 chars2 lines
Alternatively, you can use regular expressions to replace the word "dog" with "cat" in the given sentence. Here's the Python code for it:
main.py140 chars6 lines
Output:
main.py45 chars2 lines
In this code, re.sub()
is used to replace the word "dog" with "cat" using the regular expression r'\bdog\b'
. \b
denotes a word boundary.
gistlibby LogSnag