How to add a string to a paragraph after a particular word Python

Question:

I am trying to insert a string after a specific word in a paragraph.

For example:
If the word I am looking at is King in the paragraph, I want to add Arthur right after it so it will be like:

King Arthur went on a mission

for all the lines that has King instead of

King went on a mission
Asked By: JohnnySilverhand

||

Answers:

This example uses re.sub to substitute King (not followed by Arthur) with King Arthur):

import re

s = "King went on a mission"

s = re.sub(r"bKingb(?!s*Arthur)", "King Arthur", s)
print(s)

Prints:

King Arthur went on a mission
Answered By: Andrej Kesely
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.