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
- Decoders accept bytes-like objects or ASCII strings.
- With
validate=False(the default), characters outside the alphabet are discarded before the padding check. Withvalidate=True, they raisebinascii.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.
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
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.},
}
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.
- python-base64.table.json (table; CC-BY-4.0)
- python-base64.schema.json (schema; CC-BY-4.0)
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.