PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteEncode the string as bytes, pass those bytes to Python’s Base64 encoder, and decode the result as ASCII if you need a normal Python string:
import base64
text = "Hello, world!"
encoded = base64.b64encode(text.encode("utf-8")).decode("ascii")
print(encoded) # SGVsbG8sIHdvcmxkIQ==
Why does Base64 need bytes?
Base64 represents byte data using a limited set of ASCII characters; it does not directly encode Python text. A Python str is Unicode text, so first serialize it into bytes with a text encoding. Python’s base64.b64encode() accepts bytes-like data and returns encoded bytes. The final .decode("ascii") converts those Base64 bytes into a Python string. See the Python 3.14.8 Base64 documentation and codec documentation.
How to decode Base64 back into text
Reverse the operations: decode the Base64 representation to bytes, then decode those bytes with the same text encoding used to encode the original string.
decoded_text = base64.b64decode(encoded).decode("utf-8")
print(decoded_text) # Hello, world!
This round trip works when the Base64 value represents bytes originally produced from the text using UTF-8. If the data came from another system, use the text encoding that system used.
#1 Best Overall
Which text encoding should you use?
Use the encoding expected by the receiving system. UTF-8 is a practical default for general Unicode text; Python’s codec documentation describes encoding strings to bytes and decoding bytes back to strings. If an API or file format specifies a different encoding, use that consistently on both sides.
Do not use str(some_bytes) to turn encoded bytes into text. Without an encoding argument, str() produces a representation such as "b'Zoot!'", rather than decoding the byte contents. Python documents this behavior in its built-in types documentation.
Rank #2
When should you use URL-safe Base64?
Use the URL-safe variant when the receiving system expects Base64 in a URL or filesystem name:
encoded_url = base64.urlsafe_b64encode(text.encode("utf-8")).decode("ascii")
decoded_bytes = base64.urlsafe_b64decode(encoded_url)
decoded_text = decoded_bytes.decode("utf-8")
URL-safe Base64 replaces + with - and / with _. It may still include = padding. Follow the receiving system’s requirements before removing padding, and use urlsafe_b64decode() for the corresponding decode operation.
How to handle malformed or untrusted Base64
By default, base64.b64decode() uses validate=False: characters outside the Base64 alphabet are discarded before padding is checked. Set validate=True if unexpected characters should cause an error instead:
decoded_bytes = base64.b64decode(encoded_input, validate=True)
Strict validation raises binascii.Error for non-alphabet characters; incorrect padding can also raise binascii.Error. Choose permissive or strict behavior according to the input format you accept.
Base64 is not encryption
Base64 is an encoding for representing bytes as text, not a method of protecting data. Anyone who obtains a Base64 value can decode it, so do not use Base64 alone to keep confidential information secret.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →




