Replacing specific words in a string (Python)

You don’t need a regular expression for that. I would do

string = "What $noun$ is $verb$?"
print string.replace("$noun$", "the heck")

Only use regular expressions when needed. It’s generally slower.

Leave a Comment