Python Base64 Encode & Decode
Encode and decode Base64 strings online β with Python code examples, gotchas, and tips.
π 100% private β runs entirely in your browserEncode and decode Base64 strings online β with Python code examples, gotchas, and tips.
π 100% private β runs entirely in your browserPython's base64 module provides functions for encoding binary data to Base64 and decoding Base64 strings back to binary. It's part of the standard library β no pip install required.
+ and / characters.- and _ instead of + and /..encode('utf-8') before encoding and .decode('utf-8') after decoding.= to make the length a multiple of 4. Some APIs strip padding; Python's decoder handles both.import base64 text = "Hello, World!"
encoded = base64.b64encode(text.encode("utf-8")).decode("utf-8")
print(encoded) # SGVsbG8sIFdvcmxkIQ==import base64 b64_string = "SGVsbG8sIFdvcmxkIQ=="
decoded = base64.b64decode(b64_string).decode("utf-8")
print(decoded) # Hello, World!import base64 data = b"subjects?_d=42&foo=bar"
encoded = base64.urlsafe_b64encode(data).decode("utf-8")
print(encoded) # c3ViamVjdHM_X2Q9NDImZm9vPWJhcg== # Decode it back
decoded = base64.urlsafe_b64decode(encoded)
print(decoded) # b'subjects?_d=42&foo=bar'import base64 with open("image.png", "rb") as f: encoded = base64.b64encode(f.read()).decode("utf-8") # Use in a data URI
data_uri = f"data:image/png;base64,{encoded}"The most common Python 3 Base64 error. b64encode() expects bytes, not str. Always call .encode('utf-8') on your string first: base64.b64encode(my_string.encode('utf-8')).
base64.b64encode() returns a bytes object like b'SGVsbG8='. To get a regular string, chain .decode('utf-8') on the result.
Base64 is trivially reversible β anyone can decode it. Never use it to "hide" passwords, tokens, or secrets. Use proper encryption (cryptography library) for sensitive data.
Some APIs or URL parameters strip = padding. Python's b64decode handles missing padding, but other languages may not. Use urlsafe_b64encode for URL contexts.
Use base64.b64encode(string.encode('utf-8')).decode('utf-8'). The encode('utf-8') converts the string to bytes, b64encode produces Base64 bytes, and decode('utf-8') converts back to a string.
b64encode uses standard Base64 with + and / characters. urlsafe_b64encode replaces + with - and / with _, making the output safe for URLs and filenames.
Base64 is a binary-to-text encoding. Python 3 enforces the distinction between str (text) and bytes (binary data). You must encode strings to bytes first with .encode('utf-8') before Base64 encoding.
Read the file in binary mode (open('file', 'rb')), then pass the bytes to base64.b64encode(). For large files, consider reading in chunks.
Yes. All encoding and decoding happens entirely in your browser using client-side JavaScript. Your data is never sent to any server.