Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3

What is base64.b85encode(b) in Python?

Syed Muhammad Ali Mustafa

The base64.b85encode() method encodes a byte-like object into binary form using Base85 alphabets.

b85encode() method

Syntax

We can use the following code to import b85encode from base64:

from base64 import b85encode

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

base64.b85encode(b_string, pad=False)

This method returns the encoded bytes for the byte-string provided as an argument.

The argument pad is optional with a default value of False. If set to true, b_string is padded with b'\0 to keep the input length as a multiple of 4.

Code

The code below demonstrates the use of base64.b85encode().

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

# with pad at default False
enc_str = b85encode(b_str)
print('Data type of encoded \'enc_str\': ', type(enc_str))

print('Encoded string: ', enc_str)
print('length of encoded string:', len(enc_str))

# with pad set to True
enc_str2 = b85encode(b_str, pad=True)
print('Data type of encoded \'enc_str2\': ', type(enc_str2))
print('Encoded string: ', enc_str2)
print('length of encoded string 2:', len(enc_str2))

Explanation

In line 3, a byte string is initialized and stored in the variable b_str. The next line prints the 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.

In the second example – when pad is set to True in line 14 – the original byte string is padded, and then the padded string is encoded.

RELATED TAGS

python
python3

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring