Marcin Rak mrak at
Sun Jun 12 14:56:18 EDT 2016

Hi to everyone.

Let's say I have some binary data, be it whatever, in the 'data' variable.  After calling the following line

b64_encoded_data = base64.b64encode(data)

my b64_encoded_data variables holds, would you believe it, a string as bytes!.
That is, the b64_encoded_data variable is of type 'bytes' and when you peek inside it's a string (made up of what seems to be only characters that exist in Base 64).  Why isn't it a string yet?  In fact, I now on that variable have to apply the decode('utf-8') method to get a string object holding the exact same sequence of characters as was held by b64_encoded_data bytes variable.

I'm a little confused as to why I would even have to apply the
.decode('utf-8') method - why doesn't base64.b64encode provide us with a result that is a 'str'?

More information about the Python-list mailing list