How to encode text to base64 in python
Remember to import base64 and that b64encode takes bytes as an argument.
import base64
base64.b64encode(bytes('your string', 'utf-8'))
For py3
, base64 encode
and decode
string:
import base64
def b64e(s):
return base64.b64encode(s.encode()).decode()
def b64d(s):
return base64.b64decode(s).decode()
It turns out that this is important enough to get it's own module...
import base64
base64.b64encode(b'your name') # b'eW91ciBuYW1l'
base64.b64encode('your name'.encode('ascii')) # b'eW91ciBuYW1l'