The default encoding for print not dependent on sys.getdefaultencoding() , but on sys.stdout.encoding . If you run python using, for example, LANG=C or redirect the python script to a file, the encoding for stdout will be ANSI_X3.4-1968 . On the other hand, if sys.stdout is a terminal, it will use terminal encoding.
Explain what sys.getdefaultencoding() does - it is used when implicitly converting strings from / to unicode. In this example, str(u'La Pe\xf1a') with the default ASCII encoding will fail, but with the changed default encoding, it will encode the string in Latin-1. However, setting the default encoding is a terrible idea, you should always use explicit encoding if you want to switch from unicode to str .
source share