DevToolsForYou

Base64 Encoder and Decoder in Python — Code Examples

Base64 Encoder and Decoder in PythonUse the online tool →

Base64 encoding converts binary or text data into a 64-character ASCII alphabet so it can be safely transmitted over text-only channels. Here is how to encode and decode Base64 in each language using only the standard library.

Python's standard library includes the base64 module. Use b64encode and b64decode for standard Base64, and urlsafe_b64encode for URL-safe variants.

Python 3
import base64

# Encode string to Base64
text = "Hello, world!"
encoded = base64.b64encode(text.encode("utf-8")).decode("utf-8")
print(encoded)  # SGVsbG8sIHdvcmxkIQ==

# Decode Base64 to string
decoded = base64.b64decode("SGVsbG8sIHdvcmxkIQ==").decode("utf-8")
print(decoded)  # Hello, world!

# URL-safe Base64 (for JWTs, URLs)
url_safe = base64.urlsafe_b64encode(text.encode()).decode()
print(url_safe)  # SGVsbG8sIHdvcmxkIQ==
Notes & gotchas
  • b64encode and b64decode operate on bytes, not strings — encode your string to bytes first.
  • Padding characters (=) are required for decoding; add them back if stripped.
  • Use urlsafe_b64encode for tokens and URL parameters.
Try it in your browser

Need to base64 encode/decode without writing code? The Base64 Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Base64 Encode/Decode
Base64 Encoder and Decoder in other languages