title: Shift cipher in Python: error using ord
body: <p>I want to replace each character of a string by a different one, shifted over in the alphabet. I'm shifting by 2 in the example below, so a -> c, b -> d, etc.</p>
  
  <p>I'm trying to use a regular expression and the <code>sub</code> function to accomplish this, but I'm getting an error.</p>
  
  <p>This is the code that I have:</p>
  
  <pre><code>p = re.compile(r'(\w)')
  test = p.sub(chr(ord('\\1') + 2), text)
  print test
  </code></pre>
  
  <p>where the variable <code>text</code> is an input string.</p>
  
  <p>And I'm getting this error:</p>
  
  <blockquote>
    <p>TypeError: ord() expected a character, but string of length 2 found</p>
  </blockquote>
  
  <p>I think the problem is that I the <code>ord</code> function is being called on the literal string "\1"  and not on the <code>\w</code> character matched by the regular expression. What is the right way to do this?</p>