Python Reference ยท Standard Library: Math, Data and Text
The base64 module
Reference for Python's base64 module: encode bytes as Base64 text, decode it back, URL-safe variants, Base32 and Base16, padding errors and common mistakes.
The base64 module converts binary data into text made only of safe, printable ASCII characters, and back again. Base64 is used wherever bytes must travel through a text-only channel: email attachments, data URLs, JSON fields, HTTP basic authentication headers and tokens.
Encoding makes data about one third larger (every 3 bytes become 4 characters). It is an encoding, not encryption: anyone can decode it. The main functions take bytes-like input and return bytes, so convert from and to str explicitly.
import base64 encoded = base64.b64encode(b"hello") print(encoded) # b'aGVsbG8=' print(base64.b64decode(encoded)) # b'hello'
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Main functions
| Function | Description |
|---|---|
b64encode(s, altchars=None) |
Standard Base64 as bytes. altchars replaces + and /. |
b64decode(s, altchars=None, validate=False) |
Decodes bytes or an ASCII str. Raises binascii.Error on bad padding. |
urlsafe_b64encode(s) / urlsafe_b64decode(s) |
Use - and _, safe in URLs and file names. Keep the = padding. |
standard_b64encode(s) / standard_b64decode(s) |
Same as b64encode / b64decode with defaults. |
b32encode / b32decode |
Base32 (letters A-Z and 2-7); case-insensitive and unambiguous, used for one-time-password secrets. |
b16encode / b16decode |
Hexadecimal (uppercase). |
a85encode, b85encode |
Denser encodings (Ascii85 and Base85); about 25% overhead instead of 33%. |
encodebytes(s) / decodebytes(s) |
The MIME style: lines of 76 characters ending in newlines. |
encode(input, output) / decode(...) |
Work on binary file objects. |
Usage notes
- Bytes in, bytes out.
b64encode("text")raisesTypeError. Encode first (text.encode("utf-8")) and call.decode("ascii")on the result when you need astr. - Padding. Output length is always a multiple of 4, padded with
=. Many tokens (such as JWTs) strip it; add it back before decoding, as in the example. - Choose the right alphabet. Standard Base64 contains
+and/, which have special meaning in URLs. Useurlsafe_b64encodefor URLs, file names and cookies. - Lenient decoding by default. Characters outside the alphabet (spaces, newlines) are discarded unless you pass
validate=True, which is wise for untrusted input. - Not encryption or hashing. Never use it to hide secrets. For fixed-size fingerprints see
hashlib. - Size.
b64encodekeeps the whole result in memory. For large files usebase64.encode(infile, outfile), which works line by line. - Data URLs look like
"data:image/png;base64," + b64encode(png_bytes).decode(). - Python 3.13 added
base64.z85encode()andz85decode().
Related
bytes,str.encode(),hashlib,struct,uuid- Lessons: Strings, JSON in Python