

That is because I used three BACKSPACE characters and three SPACE characters to erase the 'msg'.
/463016279-56a289853df78cf7727749c5.jpg)
This is a character encoding that is based off of the mathematical construct of radix-64 or base-64 number system, but they are very different. Read on for Base-64 stuff.īase64 encoding takes 6-bit binary chunks and encodes them using the characters A-Z, a-z, 0-9, '+', '/', and '=' (some encodings use different characters in place of '+' and '/'). Originally the question title asked about Base-64 encoding. encode() method on it: Python is interpreting the string in utf-8 (the default encoding) and providing you the array of bytes that it corresponds to. That is what is happening when you take a string and call the. The most common (and default in Python 3) is utf-8, especially since it is backwards compatible with ASCII (although, as are most widely-used encodings). You can encode that string (or interpret it) in a variety of ways. In Python 3, str objects are not C-style character arrays (so they are not byte arrays), but rather, they are data structures that do not have any inherent encoding. > data = base64.b64encode(string.encode()) > data = base64.b64encode(b'data to be encoded') You need to push a bytes-like object ( bytes, bytearray, etc) to the base64.b64encode() method. See the Variants summary table at Wikipedia for an overview. In addition, some base64 variants may use characters other than + and /. * Most base64 flavours may also include a = at the end as padding. Which would be the same thing in this case. Or simpler: > encoded = b'data to be encoded' You can convert it to ascii instead, with > encoded = 'data to be encoded'.encode('ascii') In your second example: > encoded = base64.b64encode('data to be encoded')Īll the characters fit neatly into the ASCII character set, and base64 encoding is therefore actually a bit pointless. base64 has no idea what to do with Unicode data, it's not 8-bit. A string is a sequence of Unicode characters.

If you remove the b, it becomes a string. You create those in Python 3 with the b'' syntax. Base64 encoding takes 8-bit binary byte data and encodes it uses only the characters A-Z, a-z, 0-9, +, /* so it can be transmitted over channels that do not preserve all 8-bits of data, such as email.
