Decoding Base64 in Python: Practical Examples with base64 Module
Complete tutorial on using Python standard library base64 module to encode and decode strings, files, and URL-safe payloads.
Try Toolstack's Base64 Decoder
Free, instantaneous, and processes 100% locally in your browser.
Python includes a comprehensive `base64` module in its standard library that handles standard Base64, URL-safe variants, and file I/O operations without third-party dependencies.
Basic String Encoding & Decoding
Because Base64 operates on binary data, input strings must first be encoded into bytes: ```python import base64 # Encoding text to Base64 message = "Hello from Toolstack!" encoded_bytes = base64.b64encode(message.encode("utf-8")) encoded_str = encoded_bytes.decode("utf-8") print(encoded_str) # SGVsbG8gZnJvbSBUb29sc3RhY2sh # Decoding Base64 back to text decoded_bytes = base64.b64decode(encoded_str) original_text = decoded_bytes.decode("utf-8") print(original_text) # Hello from Toolstack! ```
URL-Safe Base64 in Python
For tokens and URL parameters, use `urlsafe_b64encode` and `urlsafe_b64decode`: ```python url_safe = base64.urlsafe_b64encode(b"sensitive_token_data").decode("utf-8") ```
Decoding Base64 Directly to a File
To save a Base64-encoded PDF or image directly to disk: ```python with open("output.pdf", "wb") as f: f.write(base64.b64decode(b64_pdf_string)) ```
More Guides in Base64 & Encoding
Explore More Free Tools on Toolstack
Client-side developer utilities, image converters, calculators, and educational engineering guides.
Word Counter
Count words, characters with/without spaces, sentences, and paragraphs in real time with reading speed metrics.
PDF Compressor
Reduce PDF document file size directly in your browser with zero server uploads.
Image Compressor & Resizer
Batch compress and scale JPG, PNG, and WebP images with custom quality settings.
URL Encoder & Decoder
Encode and decode query strings, special characters, and parameters with RFC 3986 percent-encoding.
JSON Validator & Formatter
Beautify, validate, minify, and auto-fix JSON syntax errors with real-time line/column diagnostics.
HEIC to JPG Converter
Convert Apple iPhone HEIC and HEIF photos to high-quality JPG directly in your browser.