I'd like to get a few opinions on the best way to replace a substring of a string with some other text. Here's an example:
I have a string, a, which could be something like "Hello my name is $name". I also have another string, b, which I want to insert into string a in the place of its substring '$name'.
I assume it would be easiest if the replaceable variable is indicated some way. I used a dollar sign, but it could be a string between curly braces or whatever you feel would work best.
Solution: Here's how I decided to do it:
from string import Template message = 'You replied to $percentageReplied of your message. ' + 'You earned $moneyMade.' template = Template(message) print template.safe_substitute( percentageReplied = '15%', moneyMade = '$20')