Python offers built-in support for Base64 encoding and decoding through the base64 module, making it simple to decode binary or textual Base64-encoded data into its original form.

Example: Decoding Base64 Data
Here’s how to decode a Base64 string using Python:

import base64
# Your Base64-encoded string
b64_data = 'ABCSSDURN'
# Convert to bytes, if it's a string
decoded_bytes = base64.b64decode(b64_data)
# If the original data was text, decode to string
try:
   decoded_string = decoded_bytes.decode('utf-8')
   print(decoded_string)
except UnicodeDecodeError:
   print("Decoded data is binary, not UTF-8 text.")

Explanation:

  • base64.b64decode() decodes the Base64-encoded input.
  • If the original data was textual (like JSON or plain text), decode the resulting bytes using .decode(‘utf-8’).
  • If the decoded data is binary (like images, PDFs, etc.), you should save it to a file instead of printing it.

Need Help With Python Development?

Work with our skilled Python developers to accelerate your project and boost its performance.

Hire Python Developers

Support On Demand!

Related Q&A