What is base64.b32encode() in Python?

The base64.b32encode() method encodes a bytes-like object into binary form using Base32 alphabets.

b32encode() method

Syntax

We can import b32encode from base64 using the following code:

from base64 import b32encode

The declaration of base64.b32encode() method is as follows:

base64.b32encode(b_string)

The method returns the encoded bytes of the binary string provided in the argument.

Code

The code below demonstrates the use of base64.b32encode():

from base64 import b32encode
b_str = b'Python is awesome!'
print('Data type of \'b_str\': ', type(b_str))
b_str = b32encode(b_str)
print('Data type of encoded \'b_str\': ', type(b_str))
print('Encoded string: ', b_str)

Explanation

In line 3, a byte string is initialized and stored in the variable, b_str. The next line prints the data type of b_str. Then in line 6, the string is encoded, and line 7 prints the type of the encoded string. Finally, in line 9, the encoded string is printed.

Copyright ©2024 Educative, Inc. All rights reserved