Base64 Encoding and Decoding in Python
Base64 is a way to represent binary data as ASCII text. It is commonly used when you need to embed binary data in text-based formats like JSON, XML, email attachments, or HTTP headers. Python’s base64 module makes this straightforward.
What is Base64?
Base64 converts binary data into a set of 64 ASCII characters (A-Z, a-z, 0-9, +, and /). Every 3 bytes of binary data become 4 Base64 characters. This makes the encoded data about 33% larger than the original, but it is safe for text-only contexts.
You will see Base64 used for:
- Embedding images in CSS or HTML
- Transmitting binary data in JSON APIs
- Encoding credentials in HTTP headers
- Storing binary files in text-based formats
Basic Encoding
The simplest way to encode data is with b64encode():
import base64
# Encode a string (must convert to bytes first)
message = "Hello, World!"
encoded = base64.b64encode(message.encode("utf-8"))
print(encoded) # b"SGVsbG8sIFdvcmxkIQ=="
The result is bytes, not a string. If you need a string:
import base64
message = "Python is awesome"
encoded = base64.b64encode(message.encode("utf-8"))
encoded_str = encoded.decode("utf-8")
print(encoded_str) # UHl0aG9uIGlzIGF3ZXNvbWU=
Basic Decoding
To decode Base64 back to bytes, use b64decode():
import base64
encoded_str = "SGVsbG8sIFdvcmxkIQ=="
decoded_bytes = base64.b64decode(encoded_str)
print(decoded_bytes) # b"Hello, World!"
# Convert bytes back to string
decoded_str = decoded_bytes.decode("utf-8")
print(decoded_str) # Hello, World!
Encoding Files
You can encode entire files, which is useful for embedding small images or sending binary data over text-only channels:
import base64
# Read and encode an image file
with open("image.png", "rb") as f:
image_data = f.read()
encoded = base64.b64encode(image_data)
print(f"Original size: {len(image_data)} bytes")
print(f"Encoded size: {len(encoded)} bytes")
# output: Original size: 1024 bytes
# output: Encoded size: 1376 bytes
Decoding Files
Similarly, you can decode Base64 data back to a file:
import base64
# Decode and save to file
encoded_data = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
with open("output.png", "wb") as f:
f.write(base64.b64decode(encoded_data))
print("File written successfully")
URL-Safe Base64
The standard Base64 uses + and /, which can cause problems in URLs. Use URL-safe encoding instead:
import base64
# Standard Base64 (can have + and /)
standard = base64.b64encode(b"hello+world/")
print(standard) # b"aGVsbG8rd29ybGQv"
# URL-safe Base64 (uses - and _ instead)
url_safe = base64.urlsafe_b64encode(b"hello+world/")
print(url_safe) # b"aGVsbG8rd29ybGQv"
Working with Padding
Base64 encoding always produces output whose length is a multiple of 4. The = characters are padding:
import base64
# Short strings need padding
print(base64.b64encode(b"a")) # b"YQ==" (2 padding chars)
print(base64.b64encode(b"ab")) # b"YWI=" (1 padding char)
print(base64.b64encode(b"abc")) # b"YWJj" (no padding)
print(base64.b64encode(b"abcd")) # b"YWJjZA==" (2 padding chars)
# You can strip padding if you handle it during decoding
def encode_no_padding(data):
return base64.b64encode(data).decode("ascii").rstrip("=")
def decode_no_padding(data):
# Add padding back before decoding
padding = 4 - (len(data) % 4)
if padding != 4:
data += "=" * padding
return base64.b64decode(data)
# Example
encoded = encode_no_padding(b"hello")
print(encoded) # aGVsbG8
decoded = decode_no_padding(encoded)
print(decoded) # b"hello"
Common Use Cases
Sending Binary Data in JSON
import base64
import json
# Imagine you have an image to send as JSON
image_data = b"\x89PNG\r\n\x1a\n..." # Actual PNG header
payload = {
"filename": "photo.png",
"data": base64.b64encode(image_data).decode("utf-8"),
"mime_type": "image/png"
}
json_str = json.dumps(payload)
print(json_str)
# output: {"filename": "photo.png", "data": "iVBORw0KGgo...", "mime_type": "image/png"}
Basic Authentication Header
import base64
username = "admin"
password = "secret123"
# Create Basic Auth header value
credentials = f"{username}:{password}"
encoded = base64.b64encode(credentials.encode("utf-8"))
auth_header = f"Basic {encoded.decode('utf-8')}"
print(auth_header)
# output: Basic YWRtaW46c2VjcmV0MTIz
Encoding API Responses
import base64
import json
# Some APIs return binary data Base64-encoded
api_response = '{"data": "SGVsbG8gZnJvbCBBUEk="}'
response = json.loads(api_response)
decoded_message = base64.b64decode(response["data"]).decode("utf-8")
print(decoded_message) # Hello from API
See Also
- The base64 Module — Official Python documentation for all base64 functions
- The json Module — Working with JSON, often paired with Base64 encoding
- The hashlib Module — Cryptographic hashing, another common data transformation