Base64 Encode in Python — Module Guide & Examples

Python's built-in base64 module provides everything you need: b64encode, b64decode, urlsafe variants, and more. This guide covers encoding strings, files, and A

Open Base64 Encoder & Decoder →

Python's built-in base64 module provides everything you need: b64encode, b64decode, urlsafe variants, and more. This guide covers encoding strings, files, and API data with working code examples.

Python developers encoding files for API uploads, handling AWS/GCP authentication, processing email attachments, or building webhook handlers frequently need Base64 operations.

Try It Now — Free, No Sign-up

Open the tool and get started instantly. No sign-up, no installation needed.

Open Base64 Encoder & Decoder Now

100% browser-based • No upload to server • No sign-up required

How to Base64 Encode in Python — Module Guide & Examples

  1. Import the module: import base64
  2. Encode string: base64.b64encode(text.encode("utf-8")).decode("ascii")
  3. Decode string: base64.b64decode(encoded).decode("utf-8")
  4. For files: read as bytes with open(file, "rb") then b64encode()
  5. For URL-safe: use base64.urlsafe_b64encode() and urlsafe_b64decode()

Pro Tips

Frequently Asked Questions

How to Base64 encode a string in Python?
import base64; encoded = base64.b64encode("hello".encode("utf-8")).decode("ascii") gives you "aGVsbG8=". The .encode/.decode steps handle the bytes conversion.
How to Base64 encode a file in Python?
with open("file.pdf", "rb") as f: encoded = base64.b64encode(f.read()).decode("ascii"). The "rb" mode reads the file as bytes.
What is urlsafe_b64encode in Python?
It replaces + with - and / with _ in the output, making it safe for URLs and filenames. Used for JWT tokens and URL parameters.
How to decode Base64 in Python?
base64.b64decode(encoded_string) returns bytes. Add .decode("utf-8") if the original was text. For binary files, write the bytes directly.

Related Tools & Guides

Ready to Use Base64 Encoder & Decoder?

Free, instant, and 100% private. No sign-up needed.

Open Base64 Encoder & Decoder