Python suggest change

base64 module

Base 64 encoding represents a common scheme for encoding binary into ASCII string format using radix 64. The base64 module is part of the standard library, which means it installs along with Python. Understanding of bytes and strings is critical to this topic and can be reviewed here. This topic explains how to use the various features and number bases of the base64 module.

Up until Python 3.4 came out, base64 encoding and decoding functions only worked with bytes or bytearray types. Now these functions accept any bytes-like object.

Encoding and Decoding Base64

To include the base64 module in your script, you must import it first:

import base64

The base64 encode and decode functions both require a bytes-like object. To get our string into bytes, we must encode it using Python’s built in encode function. Most commonly, the UTF-8 encoding is used, however a full list of these standard encodings (including languages with different characters) can be found here in the official Python Documentation. Below is an example of encoding a string into bytes:

s = "Hello World!"
b = s.encode("UTF-8")

The output of the last line would be:

b'Hello World!'

The b prefix is used to denote the value is a bytes object.

To Base64 encode these bytes, we use the base64.b64encode() function:

import base64
s = "Hello World!"
b = s.encode("UTF-8")
e = base64.b64encode(b)
print(e)

That code would output the following:

b'SGVsbG8gV29ybGQh'

which is still in the bytes object. To get a string out of these bytes, we can use Python’s decode() method with the UTF-8 encoding:

import base64
s = "Hello World!"
b = s.encode("UTF-8")
e = base64.b64encode(b)
s1 = e.decode("UTF-8")
print(s1)

The output would then be:

SGVsbG8gV29ybGQh

If we wanted to encode the string and then decode we could use the base64.b64decode() method:

import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base64 Encode the bytes
e = base64.b64encode(b)
# Decoding the Base64 bytes to string
s1 = e.decode("UTF-8")
# Printing Base64 encoded string
print("Base64 Encoded:", s1)
# Encoding the Base64 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the Base64 bytes
d = base64.b64decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)

As you may have expected, the output would be the original string:

Base64 Encoded: SGVsbG8gV29ybGQh
Hello World!

Encoding and Decoding Base32

The base64 module also includes encoding and decoding functions for Base32. These functions are very similar to the Base64 functions:

import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base32 Encode the bytes
e = base64.b32encode(b)
# Decoding the Base32 bytes to string
s1 = e.decode("UTF-8")
# Printing Base32 encoded string
print("Base32 Encoded:", s1)
# Encoding the Base32 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the Base32 bytes
d = base64.b32decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)

This would produce the following output:

Base32 Encoded: JBSWY3DPEBLW64TMMQQQ====
Hello World!

Encoding and Decoding Base16

The base64 module also includes encoding and decoding functions for Base16. Base 16 is most commonly referred to as hexadecimal. These functions are very similar to the both the Base64 and Base32 functions:

import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base16 Encode the bytes
e = base64.b16encode(b)
# Decoding the Base16 bytes to string
s1 = e.decode("UTF-8")
# Printing Base16 encoded string
print("Base16 Encoded:", s1)
# Encoding the Base16 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the Base16 bytes
d = base64.b16decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)

This would produce the following output:

Base16 Encoded: 48656C6C6F20576F726C6421
Hello World!3

Encoding and Decoding ASCII85

Adobe created it’s own encoding called ASCII85 which is similar to Base85, but has its differences. This encoding is used frequently in Adobe PDF files. These functions were released in Python version 3.4. Otherwise, the functions base64.a85encode() and base64.a85encode() are similar to the previous:

import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# ASCII85 Encode the bytes
e = base64.a85encode(b)
# Decoding the ASCII85 bytes to string
s1 = e.decode("UTF-8")
# Printing ASCII85 encoded string
print("ASCII85 Encoded:", s1)
# Encoding the ASCII85 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the ASCII85 bytes
d = base64.a85decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)

This outputs the following:

ASCII85 Encoded: 87cURD]i,"Ebo80
Hello World!

Encoding and Decoding Base85

Just like the Base64, Base32, and Base16 functions, the Base85 encoding and decoding functions are base64.b85encode() and base64.b85decode():

import base64
# Creating a string
s = "Hello World!"
# Encoding the string into bytes
b = s.encode("UTF-8")
# Base85 Encode the bytes
e = base64.b85encode(b)
# Decoding the Base85 bytes to string
s1 = e.decode("UTF-8")
# Printing Base85 encoded string
print("Base85 Encoded:", s1)
# Encoding the Base85 encoded string into bytes
b1 = s1.encode("UTF-8")
# Decoding the Base85 bytes
d = base64.b85decode(b1)
# Decoding the bytes to string
s2 = d.decode("UTF-8")
print(s2)

which outputs the following:

Base85 Encoded: NM&qnZy;B1a%^NF
Hello World!

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
32 Set
46 Enum
105 Stack
119 Mixins
121 ArcPy
127 Arrays
139 Neo4j
141 Curses
146 heapq
155 Audio
156 pyglet
158 ijson
160 base64 module
161 Flask
164 pygame
167 Gzip
168 ctypes
187 shelve
  ↑ ↓ to navigate     ↵ to select     Esc to close