Python Language The base64 Module

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

Base 64 encoding represents a common scheme for encoding binary into ASCII string format using radix 64. The base64 module is part of the standard library, which means it installs along with Python. Understanding of bytes and strings is critical to this topic and can be reviewed here. This topic explains how to use the various features and number bases of the base64 module.

Syntax

  • base64.b64encode(s, altchars=None)
  • base64.b64decode(s, altchars=None, validate=False)
  • base64.standard_b64encode(s)
  • base64.standard_b64decode(s)
  • base64.urlsafe_b64encode(s)
  • base64.urlsafe_b64decode(s)
  • base64.b32encode(s)
  • base64.b32decode(s)
  • base64.b16encode(s)
  • base64.b16decode(s)
  • base64.a85encode(b, foldspaces=False, wrapcol=0, pad=False, adobe=False)
  • base64.a85decode(b, foldpaces=False, adobe=False, ignorechars=b'\t\n\r\v')
  • base64.b85encode(b, pad=False)
  • base64.b85decode(b)

Parameters

ParameterDescription
base64.b64encode(s, altchars=None) 
sA bytes-like object
altcharsA bytes-like object of length 2+ of characters to replace the '+' and '=' characters when creating the Base64 alphabet. Extra characters are ignored.
base64.b64decode(s, altchars=None, validate=False) 
sA bytes-like object
altcharsA bytes-like object of length 2+ of characters to replace the '+' and '=' characters when creating the Base64 alphabet. Extra characters are ignored.
validateIf valide is True, the characters not in the normal Base64 alphabet or the alternative alphabet are not discarded before the padding check
base64.standard_b64encode(s) 
sA bytes-like object
base64.standard_b64decode(s) 
sA bytes-like object
base64.urlsafe_b64encode(s) 
sA bytes-like object
base64.urlsafe_b64decode(s) 
sA bytes-like object
b32encode(s) 
sA bytes-like object
b32decode(s) 
sA bytes-like object
base64.b16encode(s) 
sA bytes-like object
base64.b16decode(s) 
sA bytes-like object
base64.a85encode(b, foldspaces=False, wrapcol=0, pad=False, adobe=False) 
bA bytes-like object
foldspacesIf foldspaces is True, the character 'y' will be used instead of 4 consecutive spaces.
wrapcolThe number characters before a newline (0 implies no newlines)
padIf pad is True, the bytes are padded to a multiple of 4 before encoding
adobeIf adobe is True, the encoded sequened with be framed with '<~' and ''~>' as used with Adobe products
base64.a85decode(b, foldspaces=False, adobe=False, ignorechars=b'\t\n\r\v') 
bA bytes-like object
foldspacesIf foldspaces is True, the character 'y' will be used instead of 4 consecutive spaces.
adobeIf adobe is True, the encoded sequened with be framed with '<~' and ''~>' as used with Adobe products
ignorecharsA bytes-like object of characters to ignore in the encoding process
base64.b85encode(b, pad=False) 
bA bytes-like object
padIf pad is True, the bytes are padded to a multiple of 4 before encoding
base64.b85decode(b) 
bA bytes-like object

Remarks

Up until Python 3.4 came out, base64 encoding and decoding functions only worked with bytes or bytearray types. Now these functions accept any bytes-like object.



Got any Python Language Question?