← All notes

Python base64 - encode and decode Base64, URL-safe Base64, Base32 and Base16

Answer: b64encode() encodes a bytes-like object using Base64 and returns the encoded bytes, and b64decode() returns the decoded bytes. The module encodes binary data to printable ASCII characters and decodes such encodings back to binary data.

Examples

import base64

raw = b"\x00\xffhello"
enc = base64.b64encode(raw)              # b'AP9oZWxsbw=='
text = enc.decode("ascii")               # str for JSON or headers
base64.b64decode(text, validate=True)    # b'\x00\xffhello'

token = base64.urlsafe_b64encode(b"\xfb\xff")   # uses - and _
base64.urlsafe_b64decode(token)

base64.b32encode(b"hi")                  # b'NBUQ===='
base64.b16encode(b"hi")                  # b'6869'

try:
    base64.b64decode("not*base64", validate=True)
except ValueError:                       # binascii.Error subclasses ValueError
    print("invalid input")

Functions

| Function | Encoding | |---|---| | b64encode / b64decode | standard Base64 (RFC 4648) | | urlsafe_b64encode / urlsafe_b64decode | URL- and filesystem-safe alphabet: - instead of + and _ instead of / | | b32encode / b32decode | Base32, padded with = to a multiple of 8 | | b16encode / b16decode | Base16 (hex) | | a85encode / b85encode | Base85 variants |

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, base64 - Base16, Base32, Base64, Base85 Data Encodings; python/cpython (GitHub), Lib/base64.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, base64 - Base16, Base32, Base64, Base85 Data Encodings; python/cpython (GitHub), Lib/base64.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:3khwf1z7

iinventedlight. 2026. "Python base64 - encode and decode Base64, URL-safe Base64, Base32 and Base16." iinventedlight Library, iil:note:3khwf1z7, version of 2026-11-18. https://iinventedlight.com/library/notes/python-base64/. License: CC-BY-4.0.

@misc{iil_note_3khwf1z7,
  author = {{iinventedlight}},
  title = {{Python base64 - encode and decode Base64, URL-safe Base64, Base32 and Base16}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/python-base64/},
  note = {Permanent ID iil:note:3khwf1z7. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-base64.md.