In Python 2, two types of codecs are available; those that convert between str and unicode , and those that convert from str to str . Examples of the latter are base64 and rot13 .
There is a str.encode() method to support the latter:
'binary data'.encode('base64')
But now that it exists, people also use it for unicode str codecs; encoding can only go from unicode to str (and decoding in another way). To support them, Python will implicitly decode your str value to unicode first, using the ASCII codec, before finally encoding.
By the way, when using the str → str codec in a unicode object, Python first implicitly encodes the str code using the same ASCII codec.
In Python 3, this was solved: a) by removing the bytes.encode() and str.decode() methods (remember that bytes sorts the old str and str new unicode ), and b) by moving the encodings str → str only to the codecs module, using codecs.encode() and codecs.decode() . For which codecs converted between the same type have also been refined and updated, see the section "Specific Code Names in Python" ; note that the "text" encodings noted there, if available in Python 2, are encoded to str instead.
source share