How to Use Python Base64 Encoding and Decoding

Learn how to use Python's Base64 module for easy encoding and decoding of strings. Follow our step-by-step guide for programmers and developers.
On this page

How to Use Python Base64 Encoding and Decoding

Excerpt

Learn how to use Python’s Base64 module for easy encoding and decoding of strings. Follow our step-by-step guide for programmers and developers.

Introduction

Base64 encoding is essential for transmitting binary data over text-based networks like HTTP. Python provides easy Base64 encoding and decoding using the built-in base64 module.

In this comprehensive Python Base64 encoding guide you’ll learn:

  • What is Base64 encoding and why it’s used
  • How to import and use the base64 module
  • Step-by-step guide to encode strings and data
  • How to decode Base64 strings back to original format
  • Use cases like encrypting data and encoding images

Mastering Base64 encoding unlocks working with binary data in Python for web APIs, apps, databases, and more.

What is Base64 Encoding?

Base64 encoding converts binary data like documents, images, etc. into ASCII text characters. It represents the binary data in a way that can be transmitted over text-based protocols.

Common uses cases for Base64 encoding include:

  • Encoding data to securely transfer over networks
  • Storing complex binary data as text in databases
  • Obfuscating encrypted data for transmission
  • Embedding media like images directly in code

Base64 is not encryption itself, but is commonly used to encode encrypted data.

Importing the Base64 Module in Python

The base64 module for encoding/decoding Base64 comes pre-installed with Python.

Import it at the top of your file:

1import base64

Now you can use the base64.b64encode() and base64.b64decode() functions.

How to Encode a String to Base64 in Python

To encode a string as Base64:

  1. Import the base64 module
  2. Convert the string to bytes using .encode()
  3. Pass the bytes to base64.b64encode()

For example:

1import base64
2
3text = "IToolkit"
4encoded = base64.b64encode(text.encode("utf-8"))
5
6print(encoded) # b'SVRvb2xraXQ='

The encoded Base64 string represents the binary data as ASCII text.

When to Encode to Base64

Common use cases for Base64 encoding include:

  • Prepare binary data to transfer over networks
  • Encode strings for URL parameters or HTML
  • Store documents, images, etc. in a database
  • Obfuscate sensitive text

The free Base64 Encode verification tool is as follows:

How to Decode a Base64 String in Python

To decode a Base64 string back to the original text:

  1. Import the base64 module
  2. Pass the Base64 string to base64.b64decode()
  3. Decode the bytes to UTF-8 text

For example:

1import base64
2
3encoded = "SVRvb2xraXQ="
4decoded = base64.b64decode(encoded).decode("utf-8")
5
6print(decoded) # IToolkit

This decodes the Base64 data back into the original string.

When to Decode from Base64

Common use cases for decoding Base64 include:

  • Decode data received from APIs
  • Parse encoded strings from URLs or databases
  • Decrypt encrypted data after transmission
  • Extract embedded media or documents

The free Base64 Decode verification tool is as follows:

Conclusion

The base64 module in Python provides straightforward encoding and decoding of Base64 strings. This allows you to easily integrate Base64 conversions into your Python apps.

Base64 encoding is commonly used in web development for transmitting binary data in APIs, web apps, databases, and more. Mastering Base64 in Python is a valuable skill for any modern developer.

For more advanced usage of Base64 like encryption and encoding images, refer to:

  • Base64 Encoding for Encryption in Python
  • Working with Binary Data in Python
  • Encoding Images to Base64 Strings
  • Storing Binary Data in Django Models