Python 2.7 Unicode/IDLE confusion
Question: I've read a lot about Unicode and the various encodings/decodings in Python
2.7, but I'm still having trouble understanding why IDLE can't seem to print
the right string.

I have a unicode string:

    
    
    >>> s = u"Hey I\u2019m Bob"
    >>> print s
    Hey I'm Bob
    

However, after I encode into UTF-8, I get:

    
    
    >>> s.encode('utf-8')
    'Hey I\xe2\x80\x99m Bob'
    >>> print s.encode('utf-8')
    Hey Iâ€™m Bob
    

I can't figure out why I can't print this correctly, but I can write
s.encode('utf-8') to a text file and it comes out correctly. Not sure if this
has something to do with IDLE?


Answer: It is because IDLE's output window isn't supporting UTF-8, but printing a
Unicode string automatically tries to encode the string in the stdout
encoding. The following is from an IDLE console:

    
    
    >>> s = u"Hey I\u2019m Bob"
    >>> print s
    Hey I’m Bob
    >>> print s.encode('utf8')
    Hey Iâ€™m Bob
    >>> import sys
    >>> sys.stdout.encoding
    'cp1252'
    >>> print s.encode('cp1252')
    Hey I’m Bob
    

