In Python, decode Base64 with base64.b64decode(value, validate=True), then decode the returned bytes with .decode("utf-8", errors="strict") when the payload is text.
Python includes Base64 support in its standard library, so no third-party package is required. The important distinction is that Base64 decoding produces bytes, while text you want to display or process normally needs to be a Python string. UTF-8 is the usual bridge between those two data types.
Base64 is also not encryption. It converts bytes into a text-safe representation, but anyone with the encoded value can reverse it. Treat decoded credentials, tokens, and private files as sensitive even when they appear unreadable.

Decode Base64 text in Python
Use this workflow when a Base64 value is expected to represent text encoded as UTF-8:
- Import Python’s base64 module with import base64.
- Store the Base64 input as an ASCII string or bytes-like object.
- Call base64.b64decode(value, validate=True) to produce the original bytes.
- Call raw.decode("utf-8", errors="strict") if those bytes represent UTF-8 text.
- Handle binascii.Error for malformed Base64 and UnicodeDecodeError for bytes that are not valid UTF-8.
Worked example: `import base64`; `encoded = "SGVsbG8="`; `raw = base64.b64decode(encoded, validate=True)`; `text = raw.decode("utf-8", errors="strict")`; `print(text)`.
The output is Hello.
Why decoding happens in two stages
The first stage reverses the Base64 transformation and returns arbitrary bytes. Those bytes might represent text, an image, an archive, a JSON document, or another binary format. Python cannot assume that every decoded value should become a string, so base64.b64decode returns bytes rather than text.
The second stage is only needed when you know the content is text. Choosing UTF-8 explicitly prevents the same byte sequence from being interpreted under a different character encoding. The strict error mode also prevents malformed data from being silently replaced with an error character.
With validate=True, characters outside the expected Base64 alphabet cause a binascii.Error rather than being ignored. Incorrect padding can also cause an error. This validation applies to the Base64 layer only, which is why the separate UTF-8 decode remains necessary.
Decode UTF-8 text without corrupting Unicode
Base64 does not identify the original character encoding. If text such as café, 你好, or 😀 was encoded from UTF-8 bytes, decode those bytes with UTF-8 as well. Using another encoding may produce corrupted characters even though the Base64 transformation itself was correct.
The safest text-decoding expression is `raw.decode("utf-8", errors="strict")`. If the byte stream contains an invalid UTF-8 sequence, Python raises UnicodeDecodeError. That failure is useful because it distinguishes “this is not valid Base64” from “this is valid Base64 containing bytes that are not UTF-8 text.”
For binary payloads, keep the result as bytes. Writing raw bytes to a file or passing them to another binary-aware library is more appropriate than converting them to a string. Text decoding should only be applied when the payload’s format is known.
This UTF-8 distinction also matters in browsers. The native btoa() function accepts only Latin-1 characters and fails on text outside that range. A browser decoder that converts text to UTF-8 bytes before Base64 encoding, then validates UTF-8 after decoding, avoids that limitation.
Select the Base64 variant before decoding
The RFC 4648 Base64 specification defines the standard alphabet and its URL-safe variant. Python provides separate functions for each form.
| Python function | Alphabet and shape | Typical use | Key point |
|---|---|---|---|
| base64.b64decode | A–Z, a–z, 0–9, +, /, and optional trailing = padding | Standard RFC 4648 Base64, data URIs, and ordinary API payloads | Pass validate=True when the input should contain only valid standard characters and padding. |
| base64.urlsafe_b64decode | Uses - in place of + and _ in place of / | URL-safe values and JWT segments | The source format may omit trailing padding, so check its rules before decoding. |
For an explicitly unpadded URL-safe value, you can restore the required padding before decoding: `segment = segment + "=" * (-len(segment) % 4)`; `raw = base64.urlsafe_b64decode(segment)`. This adjustment should be limited to formats that define their values as unpadded.
A JWT payload is a practical example. Its payload segment is URL-safe Base64 containing JSON bytes, so the decoded bytes can then be interpreted as UTF-8. A JWT signature is binary data and should not automatically be decoded as text.
Preserve padding and reject malformed input
Base64 groups three input bytes into four characters. When the original byte count is not a multiple of three, the final group needs one or two = characters so the encoded length remains a multiple of four.
| Original byte count | Padding added | Reason |
|---|---|---|
| Multiple of three | None | Every three-byte group maps to four Base64 characters. |
| Remainder of one | Two = characters | The final one-byte group is padded to a complete group. |
| Remainder of two | One = character | The final two-byte group is padded to a complete group. |
Canonical RFC 4648 output includes this padding, and Python’s standard decoder expects a correctly padded value. Do not remove trailing equals signs unless the source explicitly uses an unpadded format. Conversely, do not add padding to every string without checking the format, because an incorrect number of characters changes the input structure.
Common decoding failures fall into separate categories:
- Invalid characters: A character outside the selected alphabet is rejected when validation is enabled.
- Incorrect padding: The input may not contain enough characters to form a valid final group.
- Invalid UTF-8: The Base64 layer succeeds, but the decoded bytes cannot be represented as strict UTF-8 text.
- Unexpected line breaks: Some wrapped formats include whitespace even though ordinary Base64 values do not.
Decode standard Base64 locally in a browser
If the task is a one-off decode rather than Python automation, Base64 Encode / Decode provides a direct browser workflow. It follows the standard RFC 4648 alphabet and padding rules, handles UTF-8 text, validates decoded UTF-8, and performs every conversion locally in the browser.
- Open the Base64 Encode / Decode tool.
- Choose Decode so the direction is Base64 to text.
- Paste the Base64 value into the input area. The decoded result updates instantly in the output box.
- Review the output and select Copy if you need to paste it elsewhere.
This browser workflow is not a Python API and is intended for standard Base64 text. For URL-safe input containing - or _, use Python’s urlsafe_b64decode function. Because the browser conversion stays on your device, no pasted text is uploaded to a server. For scripts, services, and automated pipelines, the Python standard library remains the more practical choice.
Recognize common Base64 payloads
Base64 appears in several familiar places:
- JSON Web Tokens: JWT header and payload segments commonly use URL-safe Base64 without padding.
- HTTP Basic authentication: The credential portion of an Authorization header is Base64-encoded, not encrypted.
- Data URIs: Images, fonts, and other resources can be embedded after a Base64 data marker.
- MIME email attachments: Binary attachment content is represented as text-safe Base64.
- JSON APIs: A binary blob may be carried as a Base64 string inside an otherwise textual JSON document.
Identifying the payload type helps you choose the correct alphabet, padding behavior, and output format. It also prevents a common mistake: decoding every result as text even when the original object is binary.
Check the result before using it
- Confirm that the input uses standard or URL-safe Base64.
- Preserve canonical padding unless the format explicitly omits it.
- Use validate=True for standard Base64 input from an untrusted source.
- Decode to bytes first, then apply strict UTF-8 only when the payload is known to be text.
- Keep binary output as bytes for file or library operations.
- Do not treat Base64 as protection for credentials, tokens, or private information.
If you're weighing options, Base64 to Hex Safely: Strict Local Byte Checks covers this in detail.
If you're weighing options, Binary to Text Round Trips: Encode, Decode, and Verify covers this in detail.