title: Using re.sub with capture group references and numbers
body: <p>Consider this simple MWE:</p>
  
  <pre><code>s = 'foo bar bar' 
  </code></pre>
  
  <p>I'd like to use <code>re.sub</code> to change this to, say:</p>
  
  <pre><code>t = 'foo1 bar1 bar1'
  </code></pre>
  
  <p>If I wanted to append a suffix <code>X</code>, this would be pretty simple:</p>
  
  <pre><code>re.sub(r'(\w+)\b', r'\1{}'.format('X'), s)
  'fooX barX barX'
  </code></pre>
  
  <p>However, the problem arises when I try to substitute numbers:</p>
  
  <pre><code>re.sub(r'(\w+)\b', r'\1{}'.format(1), s)
  </code></pre>
  
  <p>Because this gives an <code>error: invalid group reference</code>. Now, I understand why this is the case - the number is inserted into the string, and the string becomes <code>'\11'</code> which would refer to an invalid capture group.</p>
  
  <p>Is there a way to use <code>re.sub</code> and get this working?</p>
  
  <p><sub>Note that this is just a simple example, and I'm not looking for an alternate solution.</sub></p>