Python .a2b_base64()
Anonymous contributor
Published Oct 22, 2025
Contribute to Docs
The .a2b_base64() method in Python’s binascii module decodes a string of base64 encoded data. It takes an ASCII string containing base64 encoded data and converts it back into its original binary form.
Syntax
binascii.a2b_base64(string)
Parameters:
string: A bytes-like object or ASCII string containing base64-encoded data to be decoded.
Return value:
Returns the original binary data as a bytes object after decoding the base64 input.
Note: If the input string is not correctly padded to a multiple of 4, a
binascii.Errorexception is raised.
Example
In this example, .a2b_base64() decodes a base64 string representing "Hello World!" back into binary form:
import binascii# A Base64 encoded string representing "Hello World!"encoded_string = b'SGVsbG8gV29ybGQh'# Decode the Base64 string back to binarydecoded_data = binascii.a2b_base64(encoded_string)print(f"Original encoded string: {encoded_string}")print(f"Decoded binary data: {decoded_data}")print(f"Decoded as text: {decoded_data.decode('utf-8')}")
The output will be:
Original encoded string: b'SGVsbG8gV29ybGQh'
Decoded binary data: b'Hello World!'
Decoded as text: Hello World!
Codebyte Example
The following example shows how .a2b_base64() handles both valid and invalid base64 input:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours