base64

import base64
Updated March 13, 2026 · Modules
stdlib encoding binary ascii rfc-4648

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:

ParameterTypeDefaultDescription
sbytes-likeThe binary data to encode
altcharsbytesNoneAlternative 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:

ParameterTypeDefaultDescription
sbytes-likeThe Base64-encoded data to decode
altcharsbytesNoneAlternative alphabet of length 2 used for + and /
validateboolFalseIf 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:

ParameterTypeDefaultDescription
sbytes-likeThe 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:

ParameterTypeDefaultDescription
sbytes-likeThe 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:

ParameterTypeDefaultDescription
sbytes-likeThe 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:

ParameterTypeDefaultDescription
sbytes-likeThe Base16-encoded data to decode
casefoldboolFalseIf 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:

ParameterTypeDefaultDescription
bbytes-likeThe binary data to encode
padboolFalseIf 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:

ParameterTypeDefaultDescription
bbytes-likeThe Base85-encoded data to decode
padboolFalseIf 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 (when validate=True).
  • TypeError — Raised when the input is not bytes-like.
  • binascii.Incomplete — Raised when input is truncated.

See Also