title: Python's string.format() and Unicode
body: <p>I'm having a problem with Python's <code>string.format()</code> and passing Unicode strings to it.  This is similar to <a href="https://stackoverflow.com/questions/1545263/utf-8-in-python-logging-how">this older question</a>, except that in my case the test code explodes on the print, not on the <code>logging.info()</code> call. Passing the same Unicode string object to a logging handler works fine.</p>
  
  <p>This fails equally well with the older <code>%</code> formatting as well as <code>string.format()</code>. Just to make sure it was the string object that is the problem, and not print interacting badly with my terminal, I tried assigning the formatted string to a variable before printing.</p>
  
  <pre><code>def unicode_test():
      byte_string = '\xc3\xb4'
      unicode_string = unicode(byte_string, "utf-8")
      print "unicode object type: {}".format(type(unicode_string))
      output_string = "printed unicode object: {}".format(unicode_string)
      print output_string
  
  if __name__ == '__main__':
      unicode_test()
  </code></pre>
  
  <p>The string object seems to assume it's getting ASCII.  </p>
  
  <pre><code>% python -V
  Python 2.7.2
  
  % python ./unicodetest.py
  unicode object type: &lt;type 'unicode'&gt;
  Traceback (most recent call last):
    File "./unicodetest.py", line 10, in &lt;module&gt;
      unicode_test()
    File "./unicodetest.py", line 6, in unicode_test
      output_string = "printed unicode object: {}".format(unicode_string)
  UnicodeEncodeError: 'ascii' codec can't encode character u'\xf4' in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>Trying to cast <code>output_string</code> as Unicode doesn't make any difference.</p>
  
  <blockquote>
    <p>output_string = u"printed unicode object: {}".format(unicode_string)</p>
  </blockquote>
  
  <p>Am I missing something here? The documentation for the string object seems pretty clear that this should work as I'm attempting to use it.</p>