In Python 2, the string literal "" creates a byte string. Then you call .encode("utf-8") in a bytestring, Python tries to first decode it into a Unicode string using the default encoding ( ascii ) before executing .encode("utf-8") .
u"" creates a Unicode string. It will set UnicodeDecodeError as @Bleeding Fingers .
# -*- coding: utf-8 -*- print u"años luz detrás"
This can result in a UnicodeEncodeError if stdout is redirected. In this case, set the environment variable PYTHONIOENCODING .
jfs
source share