How to convert a string to Base64 in Python?

Asked

Viewed 2,938 times

4

I am starting in Python and come from PHP.

To convert a string to base 64 in PHP, I do it as follows:

base64_encode('stack overflow'); //c3RhY2sgb3ZlcmZsb3c=

And in the Python, how do I do that?

  • 1

    http://answall.com/questions/92791/existe-alguma-forma-de-converter-uma-string-para-base-64-em-javascript?answertab=votes#tab-top

1 answer

9


It’s like that. You just have to make one import:

>>> import base64
>>> encoded = base64.b64encode(b'stack overflow')
b'c3RhY2sgb3ZlcmZsb3c='

b64encode asks for an array of bytes, not a string. The b on the front make this conversion.

  • Why did you use b'stack' instead of just 'stack'? Binary?

  • 1

    Because b64encode asks for an array of bytes, not a string. O b on the front make this conversion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.