title: Replacing a substring of a string with Python
body: <p>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:</p>
  
  <p>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'.</p>
  
  <p>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.</p>
  
  <p><strong>Solution:</strong>
  Here's how I decided to do it:</p>
  
  <pre><code>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')
  </code></pre>