← Back to Blog

Python Base64 Encoding: The Complete Guide

March 20, 2026 3 min read By CodeTidy Team

Python Base64 Encoding: The Complete Guide

Have you ever tried to send binary data over a text-based protocol, only to realize that the recipient's system can't handle it? Or maybe you've struggled to embed images in HTML without using external resources? These are just a couple of scenarios where Base64 encoding comes to the rescue. In this guide, we'll explore the ins and outs of Python Base64 encoding, including encoding, decoding, and some nifty variants.

Table of Contents

  • What is Base64 Encoding?
  • Basic Encoding and Decoding
  • URL-Safe Variants
  • File Encoding and Decoding
  • Image Embedding in HTML
  • Key Takeaways
  • FAQ

What is Base64 Encoding?

Base64 is a group of binary-to-text encoding schemes that represent binary data using only a limited set of ASCII characters. This is useful when you need to transmit binary data over a text-based protocol, like email or HTTP. The encoding process involves converting the binary data into a series of 6-bit characters, which are then represented using a 64-character alphabet (hence the name "Base64").

Basic Encoding and Decoding

Python provides the base64 module, which includes functions for encoding and decoding Base64 data. Let's take a look at some examples:

import base64

# Encoding
original_data = b"Hello, World!"
encoded_data = base64.b64encode(original_data)
print(encoded_data)  # Output: b'SGVsbG8sIFdvcmxkIQ=='

# Decoding
decoded_data = base64.b64decode(encoded_data)
print(decoded_data)  # Output: b'Hello, World!'

As you can see, the b64encode function takes a bytes-like object as input and returns a Base64-encoded bytes object. The b64decode function does the opposite, taking a Base64-encoded bytes object and returning the original data.

URL-Safe Variants

The standard Base64 alphabet includes the + and / characters, which can cause issues when used in URLs. To address this, the base64 module provides URL-safe variants of the encoding and decoding functions:

import base64

# URL-safe encoding
original_data = b"Hello, World!"
encoded_data = base64.urlsafe_b64encode(original_data)
print(encoded_data)  # Output: b'SGVsbG8sIFdvcmxkIQ'

# URL-safe decoding
decoded_data = base64.urlsafe_b64decode(encoded_data)
print(decoded_data)  # Output: b'Hello, World!'

We recommend using the URL-safe variants when working with Base64 data in URLs or other text-based protocols.

File Encoding and Decoding

When working with files, you can use the base64 module in conjunction with the open function to encode and decode file data. Here's an example:

import base64

# Encoding a file
with open("input.txt", "rb") as input_file:
    original_data = input_file.read()
    encoded_data = base64.b64encode(original_data)
    with open("output.txt", "wb") as output_file:
        output_file.write(encoded_data)

# Decoding a file
with open("output.txt", "rb") as input_file:
    encoded_data = input_file.read()
    decoded_data = base64.b64decode(encoded_data)
    with open("decoded.txt", "wb") as output_file:
        output_file.write(decoded_data)

Image Embedding in HTML

One common use case for Base64 encoding is embedding images in HTML. By encoding the image data and including it in the HTML source, you can avoid the need for external resources. Here's an example:

<img src="data:image/png;base64,{{ encoded_image_data }}" />

Replace {{ encoded_image_data }} with the actual Base64-encoded image data.

Key Takeaways

  • Use the base64 module for encoding and decoding Base64 data in Python.
  • Use the URL-safe variants when working with Base64 data in URLs or other text-based protocols.
  • Use the open function to encode and decode file data.
  • Use Base64 encoding to embed images in HTML.

FAQ

Q: What is the difference between Base64 encoding and encryption?

A: Base64 encoding is a binary-to-text encoding scheme, whereas encryption is a way to protect data from unauthorized access. While Base64 encoding can make data more readable, it does not provide any security benefits.

Q: Can I use Base64 encoding for large files?

A: Yes, but be aware that the encoded data will be approximately 33% larger than the original file. This can lead to performance issues when working with large files.

Q: Is Base64 encoding suitable for all types of data?

A: No, Base64 encoding is not suitable for all types of data. For example, it is not recommended for use with compressed data, as the encoding process can actually increase the size of the data.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp