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.
Bulk CSV Data Cleaner
Clean, deduplicate, filter, reorder columns, and sanitize CSV datasets 100% in browser.
Redirect Checker & GSC Analyzer
Audit HTTP redirect chains (301, 302, 308), loops, and Google Search Console exports.
Bulk URL Status Checker
Batch check HTTP status codes (200, 301, 404, 500), latency, and hops in bulk.
Time Zone Meeting Planner
Find overlapping working hours across 2 to 10 cities and copy meeting invites instantly.
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.