base64
import base64 The base64 module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. This includes the encodings specified in RFC 4648 (Base64, Base32, and Base16) as well as Base85 encodings.
Base64 encoding is commonly used when binary data needs to be transmitted over media that only supports text — email attachments, URL parameters, JSON payloads, and HTTP headers all commonly use Base64 to represent binary data in ASCII format.
Syntax
import base64
Functions
b64encode()
Encodes a bytes-like object using Base64 and returns the encoded bytes.
Signature: base64.b64encode(s, altchars=None)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
s | bytes-like | — | The binary data to encode |
altchars | bytes | None | Alternative alphabet of length 2 for + and / characters |
Returns: bytes — The Base64-encoded data.
Example:
import base64
data = b"Hello, World!"
encoded = base64.b64encode(data)
print(encoded)
# b'SGVsbG8sIFdvcmxkIQ=='
b64decode()
Decodes a Base64-encoded bytes-like object or ASCII string and returns the decoded bytes.
Signature: base64.b64decode(s, altchars=None, validate=False)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
s | bytes-like | — | The Base64-encoded data to decode |
altchars | bytes | None | Alternative alphabet of length 2 used for + and / |
validate | bool | False | If True, non-alphabet characters raise binascii.Error |
Returns: bytes — The decoded binary data.
Example:
import base64
encoded = b'SGVsbG8sIFdvcmxkIQ=='
decoded = base64.b64decode(encoded)
print(decoded)
# b'Hello, World!'
urlsafe_b64encode()
Encodes bytes using the URL- and filesystem-safe alphabet, substituting - instead of + and _ instead of /.
Signature: base64.urlsafe_b64encode(s)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
s | bytes-like | — | The binary data to encode |
Returns: bytes — The URL-safe Base64-encoded data (may contain = padding).
Example:
import base64
data = b"hello@example.com"
encoded = base64.urlsafe_b64encode(data)
print(encoded)
# b'aGVsbG9AZXhhbXBsZS5jb20='
urlsafe_b64decode()
Decodes URL-safe Base64-encoded data.
Signature: base64.urlsafe_b64decode(s)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
s | bytes-like | — | The URL-safe Base64-encoded data to decode |
Returns: bytes — The decoded binary data.
Example:
import base64
encoded = b'aGVsbG9AZXhhbXBsZS5jb20='
decoded = base64.urlsafe_b64decode(encoded)
print(decoded)
# b'hello@example.com'
b16encode()
Encodes data using Base16 (hexadecimal).
Signature: base64.b16encode(s)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
s | bytes-like | — | The binary data to encode |
Returns: bytes — The Base16-encoded data in uppercase.
Example:
import base64
data = b"Hello"
encoded = base64.b16encode(data)
print(encoded)
# b'48656C6C6F'
b16decode()
Decodes Base16-encoded data.
Signature: base64.b16decode(s, casefold=False)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
s | bytes-like | — | The Base16-encoded data to decode |
casefold | bool | False | If True, accepts lowercase hex characters |
Returns: bytes — The decoded binary data.
Example:
import base64
encoded = b'48656C6C6F'
decoded = base64.b16decode(encoded)
print(decoded)
# b'Hello'
b85encode()
Encodes data using Base85 (as used in git-style binary diffs).
Signature: base64.b85encode(b, pad=False)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
b | bytes-like | — | The binary data to encode |
pad | bool | False | If True, pads input to a multiple of 4 bytes |
Returns: bytes — The Base85-encoded data.
Example:
import base64
data = b"Hello, World!"
encoded = base64.b85encode(data)
print(encoded)
# b'NM&qnZ!92JZ*pv8Ap'
b85decode()
Decodes Base85-encoded data.
Signature: base64.b85decode(b, pad=False)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
b | bytes-like | — | The Base85-encoded data to decode |
pad | bool | False | If True, expects padding |
Returns: bytes — The decoded binary data.
Example:
import base64
encoded = b'NM&qnZ!92JZ*pv8Ap'
decoded = base64.b85decode(encoded)
print(decoded)
# b'Hello, World!'
Common Patterns
Encoding binary file data
import base64
# Read an image file and encode it
with open("image.png", "rb") as f:
image_data = f.read()
encoded = base64.b64encode(image_data).decode("ascii")
print(f"Encoded length: {len(encoded)}")
Storing binary data in URLs
import base64
# Encode user ID for use in URLs
user_id = b"user-12345"
token = base64.urlsafe_b64encode(user_id).rstrip(b"=").decode("ascii")
print(token)
# user-12345
Removing padding for URLs
import base64
data = b"test"
encoded = base64.b64encode(data).decode("ascii")
padded_removed = encoded.rstrip("=")
print(f"Without padding: {padded_removed}")
# Without padding: dGVzdA
Working with strings
import base64
# Always encode strings to bytes first
message = "Hello, World!"
encoded = base64.b64encode(message.encode("utf-8")).decode("ascii")
print(encoded)
# Decode back to string
decoded = base64.b64decode(encoded).decode("utf-8")
print(decoded)
Errors
binascii.Error— Raised when decoding fails due to invalid padding or non-alphabet characters (whenvalidate=True).TypeError— Raised when the input is not bytes-like.binascii.Incomplete— Raised when input is truncated.