The base64.standard_b64encode()
method encodes a byte-like object into binary form using Base64 alphabets.
We can import standard_b64encode
from base64
using the following code:
from base64 import standard_b64encode
The declaration of the base64.standard_b64encode()
method is as follows:
base64.standard_b64encode(b_string)
The method returns the encoded bytes
for the byte-string provided as an argument.
The code below demonstrates the use of base64.standard_b64encode()
.
from base64 import standard_b64encode b_str = b'Python is awesome!' print('Data type of \'b_str\': ', type(b_str)) enc_str = standard_b64encode(b_str) print('Data type of encoded \'enc_str\': ', type(enc_str)) print('Encoded string: ', enc_str)
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.
RELATED TAGS
CONTRIBUTOR
View all Courses