title: how to automatically escape control-characters in a Python string
body: <p>I have strings with control-characters in them, and I want to make them visible (for printing documentation of them, for instance).</p>
  
  <p>For example, I have </p>
  
  <pre><code>dialect = csv.sniffer().sniff(csvfile.read(1024))
  </code></pre>
  
  <p>and I want to print the contents of</p>
  
  <pre><code>dialect.lineterminator
  </code></pre>
  
  <p>This obviously contains control-character(s).  They don't become printable by sticking a "\" in front of them.  I'd like to see \n \r or both, as appropriate.</p>
  
  <p>As I'm using Python 3, similar questions have proposed using str.encode, such as</p>
  
  <pre><code>    dialect.lineterminator.encode('unicode-escape')
  </code></pre>
  
  <p>but if I print this, I get </p>
  
  <pre><code>    b'\\r\\n'
  </code></pre>
  
  <p>which is, in spite of its appearance, just two bytes.  I want a unicode string such as</p>
  
  <pre><code>    "\\r\\n"
  </code></pre>
  
  <p>which is a 4-character string.  I'm not after the unicode encoding, but the escape sequence.</p>