Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base64 encodebytes v b64encode v encodestring

Tags:

python

base64

What is difference and why/when would I want to use base64.b64encode vs. base64.encodestring vs. base64.encodebytes?

From what I can tell they all basically do the same thing except that base64.encodebytes is no longer supported???

thanks

like image 484
Jay42 Avatar asked Oct 26 '25 14:10

Jay42


1 Answers

For Python 3, encodestring is the same as encodebytes except that encodestring is deprecated. In Python 2, encodebytes does not exist.

b64encode does a straight encoding, with the option of using alternate characters in the base64 alphabet in place of + and /.

encodebytes/encodestring not only encodes but inserts newlines after every 76 characters and at the end of the string. This makes it suitable for inserting directly into an email. It does not have an option for alternate characters.

Update: this seems to be an area where there's a lot of churn. encodestring appears to have been removed in Python 3.9 after being deprecated in 3.1. There's a new urlsafe_b64encode which takes a byte string and uses - and _ in place of + and /, as specified by RFC 3548.

like image 116
Mark Ransom Avatar answered Oct 29 '25 04:10

Mark Ransom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!