---
source: https://iinventedlight.com/library/notes/python-base64.md
title: "Python base64 - encode and decode Base64, URL-safe Base64, Base32 and Base16"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: base64"
tags: [python, base64, encoding, binary, urls, stdlib]
client: Librarian
sources:
  - id: S1
    title: "base64 - Base16, Base32, Base64, Base85 Data Encodings"
    publisher: Python docs
    url: https://docs.python.org/3/library/base64.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/base64.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/base64.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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

- Decoders accept bytes-like objects or ASCII strings.
- With `validate=False` (the default), characters outside the alphabet are discarded before the padding check. With `validate=True`, they raise `binascii.Error`.
- Incorrect padding raises `binascii.Error`.
- The URL-safe result can still contain `=` if padded is true (the default).
- Review the RFC 4648 security considerations section for any code deployed to production.

## See also


## Claims

- id: C1
  claim: b64encode returns the encoded bytes.
  source: S1
  evidence: "Encode the bytes-like object s using Base64 and return the encoded bytes"

- id: C2
  claim: validate=True makes non-alphabet characters raise binascii.Error.
  source: S1
  evidence: "these non-alphabet characters in the input result in a binascii.Error"

- id: C3
  claim: The module implements the RFC 4648 encodings.
  source: S2
  evidence: "Base16, Base32, Base64 (RFC 4648), Base85 and Ascii85 data encodings"

## Sources

- [S1] base64 - Base16, Base32, Base64, Base85 Data Encodings. Python docs. https://docs.python.org/3/library/base64.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] Lib/base64.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/base64.py . License: PSF-2.0. Accessed 2026-11-18.

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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
