title: How to escape $ on Python string Template class?
body: <h3>Introduction</h3>
  <p>The string module has a Template class, that lets you make substitutions in a string using a mapping object, for instance:</p>
  <pre><code>&gt;&gt;&gt; string.Template('var is $var').substitute({'var': 1})
  'var is 1'
  </code></pre>
  <p>The substitute method may raise a KeyError exception, if an attempt is made to substitute an element that is missing from the mapping, for instance</p>
  <pre><code>&gt;&gt;&gt; string.Template('var is $var and foo is $foo').substitute({'var': 1})
  KeyError: 'foo'
  </code></pre>
  <p>or may raise a ValueError, if the template string is invalid, e.g. it contains a <code>$</code> character followed by a space:</p>
  <pre><code>&gt;&gt;&gt; string.Template('$ var is $var').substitute({'var': 1})
  ValueError: Invalid placeholder in string: line 1, col 1
  </code></pre>
  <h3>The Problem</h3>
  <p>Given a template string and a mapping, I want to determine whether all place-holders in the template would be substituted. For this, I would try to make the substitution and catch any KeyError exception:</p>
  <pre><code>def check_substitution(template, mapping):
      try:
          string.Template(template).substitute(mapping)
      except KeyError:
          return False
      except ValueError:
          pass
      return True
  </code></pre>
  <p>But this doesn't work, because if the template is invalid and a ValueError is raised, subsequent KeyErrors aren't caught:</p>
  <pre><code>&gt;&gt;&gt; check_substitution('var is $var and foo is $foo', {'var': 1})
  False
  &gt;&gt;&gt; check_substitution('$ var is $var and foo is $foo', {'var': 1})
  True
  </code></pre>
  <p>but I <em>don't care</em> about ValueErrors. So, what would be the right approach to this problem?</p>