title: Mass string replace in python?
body: <p>Say I have a string that looks like this:</p>
  
  <pre><code>str = "The &amp;yquick &amp;cbrown &amp;bfox &amp;Yjumps over the &amp;ulazy dog"
  </code></pre>
  
  <p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&amp;y" and "&amp;c").  I need to replace these characters with an appropriate value that I have in a dictionary, like so:</p>
  
  <pre><code>dict = {"&amp;y":"\033[0;30m",
          "&amp;c":"\033[0;31m",
          "&amp;b":"\033[0;32m",
          "&amp;Y":"\033[0;33m",
          "&amp;u":"\033[0;34m"}
  </code></pre>
  
  <p>What is the fastest way to do this?  I could manually find all the ampersands, then loop through the dictionary to change them, but that seems slow.  Doing a bunch of regex replaces seems slow as well (I will have a dictionary of about 30-40 pairs in my actual code).</p>
  
  <p>Any suggestions are appreciated, thanks.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>As has been pointed out in comments throught this question, my dictionary is defined before runtime, and will never change during the course of the applications life cycle.  It is a list of ANSI escape sequences, and will have about 40 items in it.  My average string length to compare against will be about 500 characters, but there will be ones that are up to 5000 characters (although, these will be rare).  I am also using Python 2.6 currently.</p>
  
  <p><strong>Edit #2</strong>
  I accepted Tor Valamos answer as the correct one, as it not only gave a valid solution (although it wasn't the <em>best</em> solution), but took all others into account and did a tremendous amount of work to compare all of them.  That answer is one of the best, most helpful answers I have ever come across on StackOverflow.  Kudos to you.</p>