Skip to main content

How to Decode Base64 in Python with base64.b64decode

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Base64 turns bytes into plain text so they can travel in JSON, URLs or environment variables. base64.b64decode turns that text back into the original bytes, and with validate=True it refuses anything that isn't proper base64 instead of quietly skipping it.

Syntax​

Syntax
import base64

<data> = base64.b64decode(<text>, validate=True)
PartWhat it does
base64.b64decodeDecodes base64 and returns bytes.
<text>The base64 text, as a string or bytes.
validate=TrueRaises an error for characters outside the base64 alphabet. Without it, they're thrown away.

The base64 documentation: "Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes." Python 3.15 added three keyword-only parameters, padded, ignorechars and canonical, for stricter or looser decoding; the defaults behave as before.

The Example​

The example decodes SGVsbG8=, the base64 form of the word Hello, prints the bytes, then turns them into text.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which imports base64, decodes SGVsbG8= with base64.b64decode and validate=True, and prints data and data.decode("utf-8").

main.py
import base64

data = base64.b64decode("SGVsbG8=", validate=True)

print(data)
print(data.decode("utf-8"))
  • base64.b64decode(...) returns bytes.
  • data.decode("utf-8") reads those bytes as UTF-8 text.

Step 2: Run It​

Terminal
uv run python main.py
Output
b'Hello'
Hello

A zsh terminal where uv run python main.py prints b'Hello' and Hello.

The b in front of 'Hello' marks it as bytes; .decode turned it into an ordinary string. The command is the same in zsh, bash and PowerShell.

Why validate=True​

Without it, characters that don't belong are dropped without a word. base64.b64decode("SGV*sbG8="), with a stray *, still returned b'Hello'. With validate=True, the same mistake raises binascii.Error: Only base64 data is allowed, so broken input can't slip through.

Base64url​

Base64url, used in JWTs and URLs, writes - and _ where base64 writes + and /. b64decode with validate=True rejects those characters; decode them with base64.urlsafe_b64decode("-_8="), which returned b'\xfb\xff'. The way back is base64.b64encode(b"Hello"), which returned b'SGVsbG8='.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the = padding, as in "SGVsbG8"binascii.Error: Incorrect paddingAdd = until the length is a multiple of 4
Decoding base64url with b64decode and validate=Truebinascii.Error: Only base64 data is allowedUse base64.urlsafe_b64decode
Treating the result as a stringIt's bytes, printed as b'...'Call .decode("utf-8") when it holds text

Troubleshooting​

SymptomLikely causeFix
binascii.Error: Incorrect paddingThe text is missing its = padding, or was cut shortRestore the padding, or check the input is complete
binascii.Error: Only base64 data is allowedA character outside the alphabet, such as -, _, * or a spaceClean the input, or use the URL-safe decoder for base64url

FAQ​

Does b64decode accept a string?​

Yes, an ASCII string or bytes. It always returns bytes.

How do I get a string back out?​

data.decode("utf-8"), when the bytes hold UTF-8 text. Random bytes, such as a key, stay as bytes.

Conclusion​

base64.b64decode(text, validate=True) turns base64 into bytes and rejects anything that isn't base64. Use urlsafe_b64decode for base64url and .decode("utf-8") when the bytes are text.

Additional Resources​