How to use base64.b64decode() in Python?

The base64 module in Python provides functions for encoding and decoding data using the Base64 encoding scheme. Base64 is commonly used to encode binary data, such as images or files, into ASCII text so that it can be easily transmitted over text-based protocols like HTTP or included in JSON or XML data.

The base64.b64decode() function is used to decode data that has been encoded using the Base64 scheme. Below, we will walk through the steps to use base64.b64decode() and provide some examples.

Syntax of base64.b64decode()

base64.b64decode(s, altchars=None, validate=False)
  • s: The Base64 encoded input data as a bytes-like object or ASCII string.
  • altchars: Optional. A string of length 2 that specifies an alternative alphabet for the + and / characters.
  • validate: Optional. If set to True, non-Base64-alphabet characters in the input will raise a binascii.Error.

Basic Example

Here is a simple example of how to use base64.b64decode() to decode a Base64 encoded string:

Step 1: Import the base64 module

import base64

Step 2: Base64 encoded data

Let's assume you have some Base64 encoded data:

encoded_data = "SGVsbG8sIFdvcmxkIQ=="

Step 3: Decode the Base64 encoded data

Use the base64.b64decode() function to decode the data:

decoded_data = base64.b64decode(encoded_data) print(decoded_data)

Step 4: Convert bytes to string (if necessary)

The base64.b64decode() function returns a bytes object. If you want to convert it to a string, you need to decode it:

decoded_string = decoded_data.decode('utf-8') print(decoded_string) # Output: Hello, World!

Full Example

Here is the complete example put together:

import base64 # Base64 encoded data encoded_data = "SGVsbG8sIFdvcmxkIQ==" # Decode the Base64 encoded data decoded_data = base64.b64decode(encoded_data) # Convert bytes to string decoded_string = decoded_data.decode('utf-8') print(decoded_string) # Output: Hello, World!

Handling Different Input Types

The base64.b64decode() function can accept input as either a bytes-like object or an ASCII string. Here are examples for both types:

Decoding a Bytes-like Object

encoded_bytes = b"SGVsbG8sIFdvcmxkIQ==" decoded_data = base64.b64decode(encoded_bytes) print(decoded_data.decode('utf-8')) # Output: Hello, World!

Decoding with Alternative Characters

If the encoded data uses an alternative alphabet for + and /, specify the altchars parameter:

encoded_data = "SGVsbG8sIFdvcmxkIQ.." decoded_data = base64.b64decode(encoded_data, altchars=b'..') print(decoded_data.decode('utf-8')) # Output: Hello, World!

Handling Errors

To ensure the input is valid Base64 encoded data, use the validate parameter:

try: encoded_data = "SGVsbG8sIFdvcmxkIQ==" decoded_data = base64.b64decode(encoded_data, validate=True) print(decoded_data.decode('utf-8')) # Output: Hello, World! except base64.binascii.Error as e: print("Invalid Base64 data:", e)

Conclusion

The base64.b64decode() function is a powerful tool for decoding Base64 encoded data in Python. It is straightforward to use and can handle different input types, making it a versatile option for various applications, such as decoding data transmitted over text-based protocols or stored in text formats. By understanding how to decode Base64 data, you can efficiently work with encoded data in your Python programs.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What to wear for a Microsoft interview?
What does IDE stand for?
What is the best tool for iOS development?
Where to prepare technical interview questions with answers?
What is the best way to study software engineering?
What is Babel in React?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.