Python Glossary Русский

all Python 3binasciifunction

binascii.b2a_base64()

base64

Encodes bytes to base64 and by default appends b'\n'. The low-level basis of base64.b64encode.

Syntax

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

The trailing newline is a common surprise: the result goes into JSON or a URL and an extra '\n' appears. newline=False (3.6+) removes it, but base64.b64encode, which never adds it, is simpler. The alphabet is standard, with '+' and '/'; URLs need base64.urlsafe_b64encode. The result is bytes; a string needs .decode('ascii').

Examples

import binascii
print(binascii.b2a_base64(b'hi'))  # → b'aGk=\n'
print(binascii.b2a_base64(b'hi', newline=False))  # → b'aGk='
print(binascii.b2a_base64(b'\xfb\xff', newline=False))  # → b'+/8='

See also

Open in the full glossarydocs.python.orgReport an inaccuracy