Quick answer: Base64 looks like a random string of letters and numbers, but it is a way of writing data as text. It lets an image, a file or a piece of text travel through email, JSON or a URL without being corrupted. It is not encryption.
To decode Base64 in Python 3, import the base64 module and call the b64decode function. The result is raw bytes, so decode those bytes as UTF-8 to read the text.
import base64
value = "SGVsbG8sIFdvcmxkIQ=="
text = base64.b64decode(value).decode("utf-8")
print(text)The output is:
Hello, World!What is Base64?
Base64 turns three bytes of data into four text characters. It uses the letters A to Z, a to z, the digits 0 to 9, plus and slash. An equals sign at the end is padding, not data. The result is about one-third larger than the original, which is why a short string can grow from 13 characters to 20. The Data Size Converter can size a payload before you send it.
Compression does the opposite: it makes data smaller. Base64 only changes how the data is written, so do not use it to save space.
Base64 is also not a number base like binary or hexadecimal. The 64 refers to the number of characters in its alphabet.
Base64 is not encryption. There is no key and no secret. Anyone can decode it. Use encryption for confidentiality and a hash such as the SHA-256 hash generator for integrity.
Decode Base64 in Python 3
Python's standard library includes the base64 module. The function for decoding is b64decode. It accepts a text string or bytes and returns bytes. Most people want text, so the next step is to decode those bytes as UTF-8.
import base64
value = "SGVsbG8sIFdvcmxkIQ=="
raw = base64.b64decode(value)
text = raw.decode("utf-8")
print(text)The first line imports the module. The second line stores the Base64 string. The third decodes it to bytes. The fourth turns the bytes into text. The fifth prints the result.
If the string contains line breaks or spaces, the decoder ignores characters that are not part of Base64 by default. To reject them instead, set the validate argument to True.
base64.b64decode(value, validate=True)If you only need one string decoded and do not want to write code, the Base64 Decode tool does it in the browser.
Why Python needs two steps
The decode function returns bytes, not text. A byte is a unit of data; text is a way of interpreting those bytes. The decode method converts bytes into characters using an encoding like UTF-8. If the source used a different encoding, the text will be garbled, or Python will raise an error. This is the most common reason a decode works, but the result is unreadable.
raw = base64.b64decode(value)
print(raw)
print(raw.decode("utf-8"))The first print shows the bytes. The second shows the readable text.
Encode Base64 in Python
Encoding is the reverse. The function is b64encode. It takes bytes, not text. To encode in Python 3, start with the text, encode it as UTF-8, then encode the bytes. The result is bytes, so decode it to ASCII for JSON, headers or logs.
import base64
raw = "Hello, World!".encode("utf-8")
encoded = base64.b64encode(raw).decode("ascii")
print(encoded)The output is:
SGVsbG8sIFdvcmxkIQ==The base64 module is the Python encoder for this job. It is not a general text encoder, a compressor, or an encryption.
For URL-safe output, use urlsafe_b64encode instead. It replaces plus with a hyphen and slash with an underscore.
base64.urlsafe_b64encode(raw).decode("ascii")A common mistake is passing text directly to b64encode. It expects bytes and raises a TypeError. Encode the text first.
base64.b64encode("Hello")The error is:
TypeError: a bytes-like object is required, not 'str'If you only need to encode a string and do not want to write code, the Base64 Encode tool does it in the browser.
The same string in other environments
The image below shows the one-line difference between Python, Node, the browser, PowerShell, and the GNU command line. The input and output are the same; only the function changes.

Common errors and how to fix them
Four problems explain most failed decodes: missing padding, URL-safe characters, whitespace, and the wrong text encoding. Before you change the code, check the length after whitespace, the alphabet, and any data URL or PEM wrapper. Those checks explain most failures.

Missing padding
A Base64 string must be a multiple of four characters. If it is not, add equals signs until it is. If the length is one more than a multiple of four, the string is truncated, and padding cannot fix it.
def decode_base64(value):
value = "".join(value.split())
value += "=" * (-len(value) % 4)
return base64.b64decode(value, validate=True)URL-safe characters
URL-safe Base64 uses hyphens and underscores instead of plus and slash. Replace them before decoding, or use urlsafe_b64decode. It still expects padding.
base64.urlsafe_b64decode(value)Whitespace
Line breaks and spaces are ignored by default. If validate is True, remove them first.
value = "".join(value.split())The wrong text encoding
If every letter has a gap, the bytes are probably UTF-16LE. Decode them as UTF-16LE instead of UTF-8.
base64.b64decode(value).decode("utf-16le")
Decode a Base64 file
A .b64 file contains text, not binary. Read it as text, strip the whitespace, decode it to bytes, then write those bytes to a new file. Check the first bytes or use the file command to confirm the type.
from pathlib import Path
import base64
raw = base64.b64decode(Path("payload.b64").read_text().strip())
Path("payload.bin").write_bytes(raw)
PNG files start with 89 50 4E 47. PDF files start with 25 50 44 46. ZIP files start with 50 4B 03 04.
Decode Base64 in JavaScript
Browsers have atob, but it returns a binary string, not text. TextDecoder is the bridge from those bytes to readable text.
const binary = atob("aMOpbGxv");
const text = new TextDecoder().decode(
Uint8Array.from(binary, (char) => char.charCodeAt(0))
);
console.log(text);Node is simpler. Buffer does both steps.
const text = Buffer.from("SGVsbG8sIFdvcmxkIQ==", "base64").toString("utf8");
console.log(text);The browser function rejects URL-safe characters. Replace hyphens and underscores, add padding, then decode.
const standard = value.replace(/-/g, "+").replace(/_/g, "/");
const padded = standard + "=".repeat((4 - (standard.length % 4)) % 4);
const binary = atob(padded);
Decode Base64 on the command line
GNU base64 uses -d. macOS uses -D. The commands read from standard input and write to standard output, so they work in a pipe. Use printf rather than echo to avoid a trailing newline.
printf '%s' "$VALUE" | base64 -d
base64 -D -i input.b64 -o output.bin
PowerShell uses the .NET Convert class.
$bytes = [Convert]::FromBase64String($s)
[Text.Encoding]::UTF8.GetString($bytes)
Which method should you use?
Use Python when the value is already in a script. Use the browser decoder for a one-off string, especially when it contains a data URL, a PEM block or a JWT. Use Node when the value is already in a JavaScript service. Use the shell when the payload is a file. The decoding step is the same; the choice is about where the data already lives.
Written by Amna. Examples were run on Python 3.12.14, Node 24.13.0, and Windows PowerShell 5.1 on 25 November 2026. We checked the tool limits and encoding list against the live page. Last reviewed in November 2026.






