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 Now100% browser-based • No upload to server • No sign-up required
How to Base64 Encode in Python — Module Guide & Examples
- Import the module: import base64
- Encode string: base64.b64encode(text.encode("utf-8")).decode("ascii")
- Decode string: base64.b64decode(encoded).decode("utf-8")
- For files: read as bytes with open(file, "rb") then b64encode()
- For URL-safe: use base64.urlsafe_b64encode() and urlsafe_b64decode()
Pro Tips
- Always encode strings to bytes first: .encode("utf-8") before b64encode()
- b64encode returns bytes — use .decode("ascii") to get a string
- For large files, read and encode in chunks to avoid memory issues
- Python 3.x requires bytes input — Python 2 accepted strings directly
Frequently Asked Questions
Related Tools & Guides
Ready to Use Base64 Encoder & Decoder?
Free, instant, and 100% private. No sign-up needed.
Open Base64 Encoder & Decoder