Base64 represents bytes as ASCII text for JSON, URLs, and protocols that do not carry binary data directly. It does not compress, encrypt, or protect secrets.

Practical example

import base64

payload = b"invoice:4821"
encoded = base64.urlsafe_b64encode(payload).decode("ascii")
decoded = base64.urlsafe_b64decode(encoded.encode("ascii"))

assert decoded == payload
print(encoded)

When Base64 is appropriate

Use it only when an interface requires a textual representation. Keep str and bytes conversions explicit and agree on the alphabet with the data consumer.

Safe decoding

Limit input size before decoding. For standard Base64, b64decode(..., validate=True) rejects unexpected characters. A successful decode never makes untrusted content safe.

Keep learning

Continue with Python Unicode, Bytes, and UTF-8: Practical Guide and Python secrets: Generate Secure Tokens. A official Python documentation, accessed July 22, 2026, documents the API and behavior across versions.