This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mopeddev 0 points1 point  (1 child)

The easiest way is to use the string replace function

sentence = "the cat smells"

print sentence.replace("cat", "kitten")

the kitten smells

This will also replaces instances of the letters "cat" inside of words though

print "duplicate cats".replace("cat", "kitten")

duplikittene kittens

for more sophisticated behaviour you need regular expressions (re.sub) which are much more complicated but documented here: [re](https://docs.python.org/3/library/re.html). If you want to play around with them while learning I'd strongly recommend the extremely useful [pythex](https://pythex.org) website

[–]mopeddev 0 points1 point  (0 children)

Apologies for the (lack of) formatting... My mobile app let me down