base64.standard_b64encode / standard_b64decode
Exact aliases with a self-documenting name: standard_b64encode(s) is b64encode(s), standard_b64decode(s) is b64decode(s). No altchars, no validate.
Common call
base64.standard_b64encode(data) · base64.standard_b64decode(text)
Returns
bytes — b'aGk=' / b'hi'
Replaces
b64encode / b64decode when you want "standard alphabet" spelled out in the code
Watch out
Decoding is lenient: - and _ are silently dropped, not converted
base64.standard_b64encode(ss — The data to encode, or the Base64 text to decode.type: bytes-like (encode) · bytes-like | ASCII str (decode) · required)
→ bytes
Demo
Live evaluation
Text → UTF-8 bytes → standard Base64 (+ and / in the alphabet).
Try:
Inputs
textstrany text
Code
import base64 base64.standard_b64encode('hello'.encode())
Result
b'aGVsbG8='
"???>>>" is chosen because its bytes land on the last two alphabet positions: standard Base64 writes Pz8/Pj4+. Feed the URL-safe spelling Pz8_Pj4- to standard_b64decode and _ and - are simply dropped, leaving 6 data characters — one group short — so it raises Incorrect padding instead of decoding.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| s | bytes-like (encode) · bytes-like | ASCII str (decode) | yes | The data to encode, or the Base64 text to decode. |
Return value
bytes — Encode: the Base64 text as ASCII bytes. Decode: the original bytes.
Common patterns
Name the alphabet explicitly
Same result as b64encode — the name tells readers which alphabet the other side expects.
import base64 header = base64.standard_b64encode(raw).decode('ascii')
Accept either alphabet
Translate - and _ to + and / first; then any RFC 4648 Base64 decodes.
import base64 def decode_any(s): return base64.standard_b64decode(s.replace('-', '+').replace('_', '/'))
Examples
1. Same as b64encode
import base64
base64.standard_b64encode(b'hello') == base64.b64encode(b'hello')
Returns
True2. Encode
import base64
base64.standard_b64encode(b'\xfb\xff')
Returns
b'+/8='3. Decode
import base64
base64.standard_b64decode('+/8=')
Returns
b'\xfb\xff'4. URL-safe input fails
import base64
base64.standard_b64decode('-_8=')
Returns
binascii.Error: Invalid base64-encoded string: number of data characters (1) cannot be 1 more than a multiple of 45. No validate argument
import base64
base64.standard_b64decode('aGk=', validate=True)
Returns
TypeError: standard_b64decode() got an unexpected keyword argument 'validate'Pitfalls
1. Expecting it to convert URL-safe characters
The standard decoder treats - and _ as junk and drops them. Use urlsafe_b64decode for that alphabet.
standard_b64decode
import base64 base64.standard_b64decode('Pz8_Pj4-')
binascii.Error: Incorrect padding
urlsafe_b64decode
import base64 base64.urlsafe_b64decode('Pz8_Pj4-')
b'???>>>'
2. Needing strict checking
standard_b64decode has no validate parameter; call b64decode directly for strict mode.
lenient
import base64 base64.standard_b64decode('aG k=')
b'hi'
b64decode(validate=True)
import base64 base64.b64decode('aG k=', validate=True)
binascii.Error: Only base64 data is allowed
When to use
Use it
- Code that should say "standard alphabet" out loud, next to urlsafe_ calls
Reach for something else
- Custom alphabets or strict validation → b64encode(altchars=) / b64decode(validate=True)
- URLs and JWTs → urlsafe_b64encode / urlsafe_b64decode
Notes
CPython impl
One-line wrappers in Lib/base64.py: return b64encode(s) / return b64decode(s)
Alphabet
A–Z a–z 0–9 + / with = padding (RFC 4648 section 4)
FAQ
None in the result — standard_b64encode(s) simply returns b64encode(s). It only lacks the altchars argument, and standard_b64decode lacks altchars and validate.