Base64 in Python
How to encode and decode Base64 in Python — with copy-paste examples and a live converter to check your output.
Python ships with the base64 module. Encode bytes (not str), so encode your text to UTF-8 first, then decode the result back to a string.
Encode to Base64 in Python
import base64
encoded = base64.b64encode("Hello, World!".encode("utf-8")).decode("ascii")Decode Base64 in Python
import base64
text = base64.b64decode(encoded).decode("utf-8")Notes & gotchas
base64.b64encode takes and returns bytes, so call .encode("utf-8") on the input and .decode("ascii") on the output. For URL-safe Base64, use base64.urlsafe_b64encode / urlsafe_b64decode.
Try it live
Base64 in Python — FAQ
How do I Base64 encode a string in Python?+
Use the code shown above. base64.b64encode takes and returns bytes, so call .encode("utf-8") on the input and .decode("ascii") on the output. For URL-safe Base64, use base64.urlsafe_b64encode / urlsafe_b64decode.
How do I decode Base64 back to text in Python?+
Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.
Is Base64 encoding the same across programming languages?+
Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.
Does Base64 secure my data?+
No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.