How to convert a string to base64 in Python?

3

I'm starting in Python and I'm coming from PHP .

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

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

And in Python , how do I do this?

    
asked by anonymous 16.10.2015 / 17:18

1 answer

8

It's similar. You only have to import :

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

b64encode requests an array of bytes, not a string . b up front does this conversion.

    
16.10.2015 / 17:21