title: How does one print a Unicode character code in Python?
body: <p>I would like to print a unicode's character code, and not the actual glyph it represents in Python.  </p>
  
  <p>For example, if <code>u</code> is a list of unicode characters:</p>
  
  <pre><code>&gt;&gt;&gt; u[0]
  u'\u0103'
  &gt;&gt;&gt; print u[0]
  ă
  </code></pre>
  
  <p>I would like to output the character code as a raw string: <code>u'\u0103'</code>.</p>
  
  <p>I have tried to just print it to a file, but this doesn't work without encoding it in <code>UTF-8</code>.  </p>
  
  <pre><code>&gt;&gt;&gt; w = open('~/foo.txt', 'w')
  &gt;&gt;&gt; print&gt;&gt;w, u[0].decode('utf-8')
  
  Traceback (most recent call last):
    File "&lt;pyshell#33&gt;", line 1, in &lt;module&gt;
      print&gt;&gt;w, u[0].decode('utf-8')
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
      return codecs.utf_8_decode(input, errors, True)
  UnicodeEncodeError: 'ascii' codec can't encode character u'\u0103' in position 0: ordinal not in range(128)
  &gt;&gt;&gt; print&gt;&gt;w, u[0].encode('utf-8')
  &gt;&gt;&gt; w.close()
  </code></pre>
  
  <p>Encoding it results in the glyph <code>ă</code> being written to the file.</p>
  
  <p>How can I write the character code?  </p>