Python .b2a_base64()
Anonymous contributor
Published Oct 23, 2025
Contribute to Docs
The binascii.b2a_base64() function converts binary data into a Base64-encoded ASCII string, making it easier to transmit or store binary content such as images or files in text-based formats.
Syntax
binascii.b2a_base64(data, *, newline=True)
Parameters:
data: The binary data to encode.newline(optional): If set toFalse, omits the trailing newline character. Default isTrue.
Return value:
Returns the Base64-encoded version of the input binary data as a bytes object.
Example
In this example, binary data is encoded into a Base64-encoded bytes object using the default newline setting:
import binascii# Original binary databinary_data = b'Hello, Hacktoberfest!'# Encoding with the default newline characterencoded_data = binascii.b2a_base64(binary_data)# Printing the encoded dataprint(encoded_data)
The output of this code is:
b'SGVsbG8sIEhhY2t0b2JlcmZlc3Qh\n'
Note: If the trailing newline character is not needed, set
newline=False.
Codebyte Example
In this example, a short text is converted into its Base64-encoded representation, showing both the original and encoded data:
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