title: How to make python 3 print() utf8
body: <p>How can I make python 3 (3.1) <code>print(&quot;Some text&quot;)</code> to stdout in UTF-8, or how to output raw bytes?</p>
  <h2>Test.py</h2>
  <pre class="lang-python prettyprint-override"><code>TestText = &quot;Test - āĀēĒčČ..šŠūŪžŽ&quot; # this is UTF-8
  TestText2 = b&quot;Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd&quot; # just bytes
  print(sys.getdefaultencoding())
  print(sys.stdout.encoding)
  print(TestText)
  print(TestText.encode(&quot;utf8&quot;))
  print(TestText.encode(&quot;cp1252&quot;,&quot;replace&quot;))
  print(TestText2)
  </code></pre>
  <p>Output (in CP1257 and I replaced chars to byte values <code>[x00]</code>):</p>
  <pre class="lang-none prettyprint-override"><code>utf-8
  cp1257
  Test - [xE2][xC2][xE7][C7][xE8][xC8]..[xF0][xD0][xFB][xDB][xFE][xDE]  
  b'Test - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
  b'Test - ??????..\x9a\x8a??\x9e\x8e'
  b'Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
  </code></pre>
  <p><code>print</code> is just too smart... :D There's no point using encoded text with <code>print</code> (since it always show only representation of bytes not real bytes) and it's impossible to output bytes at all, because print anyway and always encodes it in <code>sys.stdout.encoding</code>.</p>
  <p>For example: <code>print(chr(255))</code> throws an error:</p>
  <blockquote>
  <pre><code>Traceback (most recent call last):
    File &quot;Test.py&quot;, line 1, in &lt;module&gt;
      print(chr(255));
    File &quot;H:\Python31\lib\encodings\cp1257.py&quot;, line 19, in encode
      return codecs.charmap_encode(input,self.errors,encoding_table)[0]
  UnicodeEncodeError: 'charmap' codec can't encode character '\xff' in position 0: character maps to &lt;undefined&gt;
  </code></pre>
  </blockquote>
  <p>By the way <code>print( TestText == TestText2.decode(&quot;utf8&quot;))</code> returns <code>False</code>, although print output is the same.</p>
  <hr />
  <p>How does Python 3 determine <code>sys.stdout.encoding</code> and how can I change it?</p>
  <p>I made a <code>printRAW()</code> function which works fine (actually it encodes output to UTF-8, so really it's not raw...):</p>
  <pre class="lang-python prettyprint-override"><code> def printRAW(*Text):
       RAWOut = open(1, 'w', encoding='utf8', closefd=False)
       print(*Text, file=RAWOut)
       RAWOut.flush()
       RAWOut.close()
  
   printRAW(&quot;Cool&quot;, TestText)
  </code></pre>
  <p>Output (now it print in UTF-8):</p>
  <blockquote>
  <pre><code>Cool Test - āĀēĒčČ..šŠūŪžŽ
  </code></pre>
  </blockquote>
  <p><code>printRAW(chr(252))</code> also nicely prints <code>ü</code> (in UTF-8, <code>[xC3][xBC]</code>) and without errors :)</p>
  <p>Now I'm looking for maybe better solution if there's any...</p>