How to encode and decode a string with base64 in Python

2 Answers

0 votes
import base64
 
encoded = base64.b64encode(bytes(u'Python programming', "utf-8"))
print(encoded)
 
decoded = base64.b64decode(encoded).decode("utf-8", "ignore")
print(decoded)
   
   
   
   
'''
run:
   
b'UHl0aG9uIHByb2dyYW1taW5n'
Python programming
 
'''

 



answered Jul 25, 2022 by avibootz
edited Jul 25, 2022 by avibootz
0 votes
import base64
 
string = 'Python programming'
 
encoded = base64.b64encode(bytes(string, "utf-8"))
print(encoded)
 
decoded = base64.b64decode(encoded).decode("utf-8", "ignore")
print(decoded)
   
   
   
   
'''
run:
   
b'UHl0aG9uIHByb2dyYW1taW5n'
Python programming
 
'''

 



answered Jul 25, 2022 by avibootz
edited Jul 25, 2022 by avibootz

Related questions

1 answer 197 views
2 answers 257 views
1 answer 209 views
1 answer 182 views
1 answer 210 views
...