Python .b2a_base64()

Anonymous contributor's avatar
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.

  • 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

Syntax

binascii.b2a_base64(data, *, newline=True)

Parameters:

  • data: The binary data to encode.
  • newline (optional): If set to False, omits the trailing newline character. Default is True.

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 data
binary_data = b'Hello, Hacktoberfest!'
# Encoding with the default newline character
encoded_data = binascii.b2a_base64(binary_data)
# Printing the encoded data
print(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:

Code
Output
Loading...

All contributors

Contribute to 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