import codecs
import os
import sys
output = 'bar' + '\u2192'
output = 'bar' + '\xe2\x86\x92'.decode('utf-8')
if sys.stdout.encoding == 'UTF-8':
    print(output)
else:
    output += os.linesep
if sys.version_info[0] >= 3:
    sys.stdout.buffer.write(bytes(output.encode('utf-8')))
else:
    codecs.getwriter('utf-8')(sys.stdout).write(output)
How to write utf8 to standard output in a way that works with python2 and python3