sukhvir sukhvir - 9 months ago 80
Python Question

How to encode text to base64 in python

I am trying to encode a text string to base64.

i tried doing this :

name = "your name"
print('encoding %s in base64 yields = %s\n'%(name,name.encode('base64','strict')))


But this gives me the following error:

LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs


How do I go about doing this ? ( using Python 3.4)

Answer Source

Remember to import base64 and that b64encode takes bytes as an argument.

import base64
base64.b64encode(bytes('your string', 'utf-8'))