import base64 message = "Python is fun" message_bytes = message.encode('ascii') base64_bytes = base64.b64encode(message_bytes) base64_message = base64_bytes.decode('ascii') print(base64_message)
Here is what the above code is Doing:
1. Encode the message string into bytes using the ascii encoding.
2. Use the base64 module to encode the message bytes into base64 bytes.
3. Decode the base64 bytes into a base64 string using the ascii encoding.