Python Base64 Encode & Decode

Encode and decode Base64 strings online β€” with Python code examples, gotchas, and tips.

πŸ”’ 100% private β€” runs entirely in your browser
or try sample data

Base64 Encoding in Python

Python'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.

  • b64encode / b64decode β€” standard Base64 encoding and decoding with + and / characters.
  • urlsafe_b64encode / urlsafe_b64decode β€” URL-safe variant using - and _ instead of + and /.
  • Bytes in, bytes out β€” Python 3 enforces the bytes/str distinction. Always call .encode('utf-8') before encoding and .decode('utf-8') after decoding.
  • Padding β€” Base64 output is padded with = to make the length a multiple of 4. Some APIs strip padding; Python's decoder handles both.

Python Base64 Code Examples

Encode a String to Base64

import base64 text = "Hello, World!"
encoded = base64.b64encode(text.encode("utf-8")).decode("utf-8")
print(encoded) # SGVsbG8sIFdvcmxkIQ==

Decode Base64 Back to a String

import base64 b64_string = "SGVsbG8sIFdvcmxkIQ=="
decoded = base64.b64decode(b64_string).decode("utf-8")
print(decoded) # Hello, World!

URL-Safe Base64 Encoding

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'

Encode a File to Base64

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}"

Python Base64 Gotchas

TypeError: a bytes-like object is required

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')).

b64encode returns bytes, not str

base64.b64encode() returns a bytes object like b'SGVsbG8='. To get a regular string, chain .decode('utf-8') on the result.

Base64 is encoding, not encryption

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.

Padding characters (=) may cause issues

Some APIs or URL parameters strip = padding. Python's b64decode handles missing padding, but other languages may not. Use urlsafe_b64encode for URL contexts.

Frequently Asked Questions

How do I Base64 encode a string in Python?

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.

What is the difference between b64encode and urlsafe_b64encode?

b64encode uses standard Base64 with + and / characters. urlsafe_b64encode replaces + with - and / with _, making the output safe for URLs and filenames.

Why does Python base64 require bytes, not strings?

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.

How do I Base64 encode a file in Python?

Read the file in binary mode (open('file', 'rb')), then pass the bytes to base64.b64encode(). For large files, consider reading in chunks.

Is this tool safe for encoding sensitive data?

Yes. All encoding and decoding happens entirely in your browser using client-side JavaScript. Your data is never sent to any server.