Menu Close

Python Base64 Module Tutorial

Python base64 Module

Here we are going to learn about the Python base64 module. Base64 encoding allows us to convert bytes containing binary data or text data to ASCII characters.

In this guide, we will see what is Python base64 module as well as we will see how to binary string to base64 string and how to decode base64 string to normal string.

What is Python Base64 Module?

In Python, the Base64 module is a Python built-in module that means you don’t need to install it by using the pip command. Python base64 module provides functions for encoding binary data to printable ASCII characters and decoding such encoding back to binary data.

What is Base64 encoding?

Base64 encoding is a type of conversion of bytes into ASCII Characters. The name of this encoding comes directly from the mathematical definition of bases – we have 64 characters that represent numbers, uppercase, lowercase, and new line.

The Base64 character set contains:

  • 26 Lower Case
  • 26 Upper Case
  • 10 Numbers
  • + and – for a new line.

How does Base64 encoding work?

Here we will explain how base64 encoding works by converting text data.
To convert a string to base64 characters the following steps should be followed.

  • Get the ASCII value of each character.
  • Compute 8-bit binary equivalent of ASCII value.
  • Convert 8-bit characters into chunks of 6-bit by re-grouping the digits.
  • Convert the 6-bit binary groups to their respective decimal values.
  • Using the Base64 encoding table, assign the respective base64 characters for each decimal value.
Python base64 module
Image Source: Wikipedia

Encoding String with Python

The Python base64 module allows us to convert encode and decode information easily. To use the Python built-in base64 module, we have to import the base64 module using the import keyword.
First, we have to convert the string to a byte-like object and then encode it.

Example:

import base64
mystr = 'Programming Funda is best place to learn programming'
mystr_bytes = mystr.encode("ascii")
base64_bytes = base64.b64encode(mystr_bytes)
base64_string = base64_bytes.decode("ascii")
print(base64_string )

Output

UHJvZ3JhbW1pbmcgRnVuZGEgaXMgdGhlIGJlc3QgcGxhY2UgdG8gbGVhcm4gdG8gcHJvZ3JhbS4=

Decoding String with Python

Decoding a base64 string is the process of the reverse of encoding. The Python base64 module also provides a facility to decode an encoded string. Firstly we have to convert encoded strings to bytes-like objects and then convert them into a string.

Example

import base64
base64_str = 'UHJvZ3JhbW1pbmcgRnVuZGEgaXMgdGhlIGJlc3QgcGxhY2UgdG8gbGVhcm4gdG8gcHJvZ3JhbS4='
mystr_bytes = base64_str.encode("ascii")

base64_string_bytes = base64.b64decode(mystr_bytes)
normal_string = base64_string_bytes.decode('ascii')
print(normal_string)

Output

Programming Funda is the best place to learn to program.

Python base64 Module Functions

base64.standard_base64encode(s) is used to encode byte like using standard base64 alphabet and return encode bytes.

Example

import base64
mystr = 'Programming Funda is the best place to learn to program.'
mystr_bytes = mystr.encode("ascii")

base64_bytes = base64.standard_b64encode(mystr_bytes)
base64_string = base64_bytes.decode("ascii")
print(base64_string)

Output

UHJvZ3JhbW1pbmcgRnVuZGEgaXMgdGhlIGJlc3QgcGxhY2UgdG8gbGVhcm4gdG8gcHJvZ3JhbS4=

base64.standard_base64decode(s)

Decode bytes-like objects or ASCII strings using the standard Base64 alphabet and return the decoded bytes.

Example

import base64
base64_str = 'UHJvZ3JhbW1pbmcgRnVuZGEgaXMgdGhlIGJlc3QgcGxhY2UgdG8gbGVhcm4gdG8gcHJvZ3JhbS4='
mystr_bytes = base64_str.encode("ascii")

base64_string_bytes = base64.standard_b64decode(mystr_bytes)
normal_string = base64_string_bytes.decode('ascii')
print(normal_string)

Output

Programming Funda is the best place to learn to program.

base64.urlsafe_base64encode(s)

base64.urlsafe_b64encode(s) method is used to encode byte-like objects using the URL filesystem safe alphabet.

Example

import base64
mystr = 'Programming Funda is the best place to learn programming++'
mystr_bytes = mystr.encode("ascii")

base64_bytes = base64.urlsafe_b64encode(mystr_bytes)
base64_string = base64_bytes.decode('ascii')
print(base64_string)

Output

UHJvZ3JhbW1pbmcgRnVuZGEgaXMgYmVzdCBwbGFjZSB0byBsZWFybiBwcm9ncmFtbWluZysr

base64.urlsafe_base64decode(s)

Decode bytes-like objects or ASCII strings using the URL and filesystem-safe alphabet.

Example

import base64
base64_str = 'UHJvZ3JhbW1pbmcgRnVuZGEgaXMgYmVzdCBwbGFjZSB0byBsZWFybiBwcm9ncmFtbWluZysr'
mystr_bytes = base64_str.encode("ascii")

base64_string_bytes = base64.urlsafe_b64decode(mystr_bytes)
normal_string = base64_string_bytes.decode('ascii')
print(normal_string)

Output

Programming Funda is the best place to learn programming++

Conclusion:

In this tutorial, you have learned about the Python base64 module to convert binary or text data to ASCII characters and vice-versa. Python base64 module provides lots of functions that are sued to convert binary data to ASCII characters.

I hope this Python base64 module tutorial will help you. if you like this article, please comment and share it with your friends who want to learn Python programming from scratch to advanced.

For More Information:- Click Here

Python JSON Module
Python string Module

Related Posts