import base64
coded_string = '''Q5YACgA...'''
base64.b64decode(coded_string)

worked for me. At the risk of pasting an offensively-long result, I got:

>>> base64.b64decode(coded_string)
2: 'C\x96\x00\n\x00\x00\x00\x00C\x96\x00\x1b\x00\x00\x00\x00C\x96\x00-\x00\x00\x00\x00C\x96\x00?\x00\x00\x00\x00C\x96\x07M\x00\x00\x00\x00C\x96\x07_\x00\x00\x00\x00C\x96\x07p\x00\x00\x00\x00C\x96\x07\x82\x00\x00\x00\x00C\x96\x07\x94\x00\x00\x00\x00C\x96\x07\xa6Cq\xf0\x7fC\x96\x07\xb8DJ\x81\xc7C\x96\x07\xcaD\xa5\x9dtC\x96\x07\xdcD\xb6\x97\x11C\x96\x07\xeeD\x8b\x8flC\x96\x07\xffD\x03\xd4\xaaC\x96\x08\x11B\x05&\xdcC\x96\x08#\x00\x00\x00\x00C\x96\x085C\x0c\xc9\xb7C\x96\x08GCy\xc0\xebC\x96\x08YC\x81\xa4xC\x96\x08kC\x0f@\x9bC\x96\x08}\x00\x00\x00\x00C\x96\x08\x8e\x00\x00\x00\x00C\x96\x08\xa0\x00\x00\x00\x00C\x96\x08\xb2\x00\x00\x00\x00C\x96\x86\xf9\x00\x00\x00\x00C\x96\x87\x0b\x00\x00\x00\x00C\x96\x87\x1d\x00\x00\x00\x00C\x96\x87/\x00\x00\x00\x00C\x96\x87AA\x0b\xe7PC\x96\x87SCI\xf5gC\x96\x87eC\xd4J\xeaC\x96\x87wD\r\x17EC\x96\x87\x89D\x00F6C\x96\x87\x9bC\x9cg\xdeC\x96\x87\xadB\xd56\x0cC\x96\x87\xbf\x00\x00\x00\x00C\x96\x87\xd1\x00\x00\x00\x00C\x96\x87\xe3\x00\x00\x00\x00C\x96\x87\xf5\x00\x00\x00\x00C\x9cY}\x00\x00\x00\x00C\x9cY\x90\x00\x00\x00\x00C\x9cY\xa4\x00\x00\x00\x00C\x9cY\xb7\x00\x00\x00\x00C\x9cY\xcbC\x1f\xbd\xa3C\x9cY\xdeCCz{C\x9cY\xf1CD\x02\xa7C\x9cZ\x05C+\x9d\x97C\x9cZ\x18C\x03R\xe3C\x9cZ,\x00\x00\x00\x00C\x9cZ?
[stuff omitted as it exceeded SO's body length limits]
\xbb\x00\x00\x00\x00D\xc5!7\x00\x00\x00\x00D\xc5!\xb2\x00\x00\x00\x00D\xc7\x14x\x00\x00\x00\x00D\xc7\x14\xf6\x00\x00\x00\x00D\xc7\x15t\x00\x00\x00\x00D\xc7\x15\xf2\x00\x00\x00\x00D\xc7\x16pC5\x9f\xf9D\xc7\x16\xeeC[\xb5\xf5D\xc7\x17lCG\x1b;D\xc7\x17\xeaB\xe3\x0b\xa6D\xc7\x18h\x00\x00\x00\x00D\xc7\x18\xe6\x00\x00\x00\x00D\xc7\x19d\x00\x00\x00\x00D\xc7\x19\xe2\x00\x00\x00\x00D\xc7\xfe\xb4\x00\x00\x00\x00D\xc7\xff3\x00\x00\x00\x00D\xc7\xff\xb2\x00\x00\x00\x00D\xc8\x001\x00\x00\x00\x00'
Answer from Blair Conrad on Stack Overflow
🌐
Python
docs.python.org › 3 › library › base64.html
base64 — Base16, Base32, Base64, Base85 Data Encodings
Encode the bytes-like object s, which can contain arbitrary binary data, and return bytes containing the base64-encoded data, with newlines (b'\n') inserted after every 76 bytes of output, and ensuring that there is a trailing newline, as per RFC 2045 (MIME). Added in version 3.1. ... >>> import base64 >>> encoded = base64.b64encode(b'data to be encoded') >>> encoded b'ZGF0YSB0byBiZSBlbmNvZGVk' >>> data = base64.b64decode(encoded) >>> data b'data to be encoded'
🌐
PyPI
pypi.org › project › pybase64
pybase64 · PyPI
To get the fastest decoding, it is recommended to use the pybase64.b64decode and validate=True when possible. import pybase64 print(pybase64.b64encode(b'>>>foo???', altchars='_:')) ...
      » pip install pybase64
    
Published   Dec 06, 2025
Version   1.4.3
Top answer
1 of 10
450
import base64
coded_string = '''Q5YACgA...'''
base64.b64decode(coded_string)

worked for me. At the risk of pasting an offensively-long result, I got:

>>> base64.b64decode(coded_string)
2: 'C\x96\x00\n\x00\x00\x00\x00C\x96\x00\x1b\x00\x00\x00\x00C\x96\x00-\x00\x00\x00\x00C\x96\x00?\x00\x00\x00\x00C\x96\x07M\x00\x00\x00\x00C\x96\x07_\x00\x00\x00\x00C\x96\x07p\x00\x00\x00\x00C\x96\x07\x82\x00\x00\x00\x00C\x96\x07\x94\x00\x00\x00\x00C\x96\x07\xa6Cq\xf0\x7fC\x96\x07\xb8DJ\x81\xc7C\x96\x07\xcaD\xa5\x9dtC\x96\x07\xdcD\xb6\x97\x11C\x96\x07\xeeD\x8b\x8flC\x96\x07\xffD\x03\xd4\xaaC\x96\x08\x11B\x05&\xdcC\x96\x08#\x00\x00\x00\x00C\x96\x085C\x0c\xc9\xb7C\x96\x08GCy\xc0\xebC\x96\x08YC\x81\xa4xC\x96\x08kC\x0f@\x9bC\x96\x08}\x00\x00\x00\x00C\x96\x08\x8e\x00\x00\x00\x00C\x96\x08\xa0\x00\x00\x00\x00C\x96\x08\xb2\x00\x00\x00\x00C\x96\x86\xf9\x00\x00\x00\x00C\x96\x87\x0b\x00\x00\x00\x00C\x96\x87\x1d\x00\x00\x00\x00C\x96\x87/\x00\x00\x00\x00C\x96\x87AA\x0b\xe7PC\x96\x87SCI\xf5gC\x96\x87eC\xd4J\xeaC\x96\x87wD\r\x17EC\x96\x87\x89D\x00F6C\x96\x87\x9bC\x9cg\xdeC\x96\x87\xadB\xd56\x0cC\x96\x87\xbf\x00\x00\x00\x00C\x96\x87\xd1\x00\x00\x00\x00C\x96\x87\xe3\x00\x00\x00\x00C\x96\x87\xf5\x00\x00\x00\x00C\x9cY}\x00\x00\x00\x00C\x9cY\x90\x00\x00\x00\x00C\x9cY\xa4\x00\x00\x00\x00C\x9cY\xb7\x00\x00\x00\x00C\x9cY\xcbC\x1f\xbd\xa3C\x9cY\xdeCCz{C\x9cY\xf1CD\x02\xa7C\x9cZ\x05C+\x9d\x97C\x9cZ\x18C\x03R\xe3C\x9cZ,\x00\x00\x00\x00C\x9cZ?
[stuff omitted as it exceeded SO's body length limits]
\xbb\x00\x00\x00\x00D\xc5!7\x00\x00\x00\x00D\xc5!\xb2\x00\x00\x00\x00D\xc7\x14x\x00\x00\x00\x00D\xc7\x14\xf6\x00\x00\x00\x00D\xc7\x15t\x00\x00\x00\x00D\xc7\x15\xf2\x00\x00\x00\x00D\xc7\x16pC5\x9f\xf9D\xc7\x16\xeeC[\xb5\xf5D\xc7\x17lCG\x1b;D\xc7\x17\xeaB\xe3\x0b\xa6D\xc7\x18h\x00\x00\x00\x00D\xc7\x18\xe6\x00\x00\x00\x00D\xc7\x19d\x00\x00\x00\x00D\xc7\x19\xe2\x00\x00\x00\x00D\xc7\xfe\xb4\x00\x00\x00\x00D\xc7\xff3\x00\x00\x00\x00D\xc7\xff\xb2\x00\x00\x00\x00D\xc8\x001\x00\x00\x00\x00'
2 of 10
224

Python 3 (and 2)

import base64
a = 'eW91ciB0ZXh0'
base64.b64decode(a)

Python 2

A quick way to decode it without importing anything:

'eW91ciB0ZXh0'.decode('base64')

or more descriptive

>>> a = 'eW91ciB0ZXh0'
>>> a.decode('base64')
'your text'
🌐
Python Engineer
python-engineer.com › posts › base64-module
The base64 Module in Python - Python Engineer
June 21, 2022 - In the above example, first we convert the input string to byte-like objects and then encode those byte-like objects to base64 format. Decoding base64 string is opposite to that of encoding. The base64 module provides the b64decode() function which decodes the Base64 encoded bytes-like object or ASCII string and returns the decoded bytes. Let's see how to use this function. import base64 base64_string = "UHl0aG9uIGlzIGEgcHJvZ3JhbW1pbmcgbGFuZ3VhZ2U=" base64_bytes = base64_string.encode('ascii') data_bytes = base64.b64decode(base64_bytes) data = data_bytes.decode('ascii') print("Decoded Data:", data) # Output: Decoded Data: Python is a programming language
🌐
W3Schools
w3schools.com › python › ref_module_base64.asp
Python base64 Module
Python Examples Python Compiler ... Training · ❮ Standard Library Modules · Encode and decode a string using Base64: import base64 s = "Linus".encode() b = base64.b64encode(s) print(b) print(base64.b64decode(b).decode()) ...
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Type the following code into the Python file: import base64 base64_img = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAA' \ 'LEwEAmpwYAAAB1klEQVQ4jY2TTUhUURTHf+fy/HrjhNEX2KRGiyIXg8xgSURuokX' \ 'LxFW0qDTaSQupkHirthK0qF0WQQQR0UCbwCQyw8KCiDbShEYLJQdmpsk3895p4aS' \ 'v92ass7pcf...
🌐
Stack Overflow
stackoverflow.com › questions › 48578807 › how-to-import-base64-in-python
How to import base64 in Python - Stack Overflow
https://community.tibco.com/qu... to request clarification or add additional context in comments. ... Download the "base64" tar ball from the internet....
🌐
Base64.Guru
base64.guru › home › developers › python
Python Base64 Encode | Python | Developers | Base64
from base64 import b64encode data = bytes('€100', 'utf-8') print(b64encode(data)) #-> b'4oKsMTAw' Example #5 (TypeError on missing required argument): # Python 2.7 - b64encode() takes at least 1 argument (0 given) # Python 3.6 - b64encode() missing 1 required positional argument: 's' ... # Python 2.7 - b2a_base64() argument 1 must be string or buffer, not int # Python 3.6 - a bytes-like object is required, not 'int'
Find elsewhere
🌐
Python Module of the Week
pymotw.com › 2 › base64
base64 – Encode binary data into ASCII characters - Python Module of the Week
import base64 original_string = 'This is the data, in the clear.' print 'Original:', original_string encoded_string = base64.b32encode(original_string) print 'Encoded :', encoded_string decoded_string = base64.b32decode(encoded_string) print 'Decoded :', decoded_string · $ python base64_base32.py ...
🌐
iO Flood
ioflood.com › blog › python-base64-encode
Python Base64 Encode | b64encode() Function Guide
February 1, 2024 - To encode data using base64 in ... input and returns the base64 encoded version of the input data. ... import base64 data = b'Hello' encoded_data = ......
Top answer
1 of 5
363

base64 encoding takes 8-bit binary byte data and encodes it uses only the characters A-Z, a-z, 0-9, +, /* so it can be transmitted over channels that do not preserve all 8-bits of data, such as email.

Hence, it wants a string of 8-bit bytes. You create those in Python 3 with the b'' syntax.

If you remove the b, it becomes a string. A string is a sequence of Unicode characters. base64 has no idea what to do with Unicode data, it's not 8-bit. It's not really any bits, in fact. :-)

In your second example:

>>> encoded = base64.b64encode('data to be encoded')

All the characters fit neatly into the ASCII character set, and base64 encoding is therefore actually a bit pointless. You can convert it to ascii instead, with

>>> encoded = 'data to be encoded'.encode('ascii')

Or simpler:

>>> encoded = b'data to be encoded'

Which would be the same thing in this case.


* Most base64 flavours may also include a = at the end as padding. In addition, some base64 variants may use characters other than + and /. See the Variants summary table at Wikipedia for an overview.

2 of 5
252

Short Answer

You need to push a bytes-like object (bytes, bytearray, etc) to the base64.b64encode() method. Here are two ways:

>>> import base64
>>> data = base64.b64encode(b'data to be encoded')
>>> print(data)
b'ZGF0YSB0byBiZSBlbmNvZGVk'

Or with a variable:

>>> import base64
>>> string = 'data to be encoded'
>>> data = base64.b64encode(string.encode())
>>> print(data)
b'ZGF0YSB0byBiZSBlbmNvZGVk'

Why?

In Python 3, str objects are not C-style character arrays (so they are not byte arrays), but rather, they are data structures that do not have any inherent encoding. You can encode that string (or interpret it) in a variety of ways. The most common (and default in Python 3) is utf-8, especially since it is backwards compatible with ASCII (although, as are most widely-used encodings). That is what is happening when you take a string and call the .encode() method on it: Python is interpreting the string in utf-8 (the default encoding) and providing you the array of bytes that it corresponds to.

Base-64 Encoding in Python 3

Originally the question title asked about Base-64 encoding. Read on for Base-64 stuff.

base64 encoding takes 6-bit binary chunks and encodes them using the characters A-Z, a-z, 0-9, '+', '/', and '=' (some encodings use different characters in place of '+' and '/'). This is a character encoding that is based off of the mathematical construct of radix-64 or base-64 number system, but they are very different. Base-64 in math is a number system like binary or decimal, and you do this change of radix on the entire number, or (if the radix you're converting from is a power of 2 less than 64) in chunks from right to left.

In base64 encoding, the translation is done from left to right; those first 64 characters are why it is called base64 encoding. The 65th '=' symbol is used for padding, since the encoding pulls 6-bit chunks but the data it is usually meant to encode are 8-bit bytes, so sometimes there are only two or 4 bits in the last chunk.

Example:

>>> data = b'test'
>>> for byte in data:
...     print(format(byte, '08b'), end=" ")
...
01110100 01100101 01110011 01110100
>>>

If you interpret that binary data as a single integer, then this is how you would convert it to base-10 and base-64 (table for base-64):

base-2:  01 110100 011001 010111 001101 110100 (base-64 grouping shown)
base-10:                            1952805748
base-64:  B      0      Z      X      N      0

base64 encoding, however, will re-group this data thusly:

base-2:  011101  000110  010101 110011 011101 00(0000) <- pad w/zeros to make a clean 6-bit chunk
base-10:     29       6      21     51     29      0
base-64:      d       G       V      z      d      A

So, 'B0ZXN0' is the base-64 version of our binary, mathematically speaking. However, base64 encoding has to do the encoding in the opposite direction (so the raw data is converted to 'dGVzdA') and also has a rule to tell other applications how much space is left off at the end. This is done by padding the end with '=' symbols. So, the base64 encoding of this data is 'dGVzdA==', with two '=' symbols to signify two pairs of bits will need to be removed from the end when this data gets decoded to make it match the original data.

Let's test this to see if I am being dishonest:

>>> encoded = base64.b64encode(data)
>>> print(encoded)
b'dGVzdA=='

Why use base64 encoding?

Let's say I have to send some data to someone via email, like this data:

>>> data = b'\x04\x6d\x73\x67\x08\x08\x08\x20\x20\x20'
>>> print(data.decode())
   
>>> print(data)
b'\x04msg\x08\x08\x08   '
>>>

There are two problems I planted:

  1. If I tried to send that email in Unix, the email would send as soon as the \x04 character was read, because that is ASCII for END-OF-TRANSMISSION (Ctrl-D), so the remaining data would be left out of the transmission.
  2. Also, while Python is smart enough to escape all of my evil control characters when I print the data directly, when that string is decoded as ASCII, you can see that the 'msg' is not there. That is because I used three BACKSPACE characters and three SPACE characters to erase the 'msg'. Thus, even if I didn't have the EOF character there the end user wouldn't be able to translate from the text on screen to the real, raw data.

This is just a demo to show you how hard it can be to simply send raw data. Encoding the data into base64 format gives you the exact same data but in a format that ensures it is safe for sending over electronic media such as email.

🌐
Real Python
realpython.com › ref › stdlib › base64
base64 | Python Standard Library – Real Python
The Python base64 module provides functions for encoding binary data as printable ASCII characters and decoding those representations back to binary. It implements the Base16, Base32, and Base64 encodings defined in RFC 4648, as well as the ...
🌐
Base64 Encode
base64encode.net › python-base64-b64encode
Python base64.b64encode() - Base64 Encode
This allows an application to e.g. generate URL or filesystem safe Base64 strings. The default is None, for which the standard Base64 alphabet is used. The encoded string is returned. <<< import base64 <<< encoded = base64.b64encode('base64 encoded string') <<< encoded 'YmFzZTY0IGVuY29kZWQgc3RyaW5n'
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › decode-base64-data-in-python
How to Decode Base64 Data in Python Easily
June 24, 2025 - Example: Decoding Base64 Data Here’s how to decode a Base64 string using Python: import base64 # Your Base64-encoded string b64_data = 'ABCSSDURN' # Convert to bytes, if it's a string decoded_bytes = base64.b64decode(b64_data) # If the original ...
🌐
AskPython
askpython.com › python › examples › decoding-base64-data
Decoding Base64 Data in Python - AskPython
April 29, 2026 - I start with a variable holding the encoded text as a string, encode it to bytes using ASCII, then pass those bytes to b64decode. Description: Basic base64 decode from string to string. import base64 encoded = "QXNrUHl0aG9uLmNvb...
🌐
Base64
base64.dev › home › articles › base64 in python
Base64 Encoding & Decoding in Python — base64.dev
May 28, 2026 - import base64 def encode_base64(text: str) -> str: """Encode a Unicode string to Base64.""" return base64.b64encode(text.encode("utf-8")).decode("ascii") def decode_base64(b64: str) -> str: """Decode a Base64 string back to Unicode text.""" return base64.b64decode(b64).decode("utf-8") print(encode_base64("Hello 🌍")) # "SGVsbG8g8J+MjQ==" print(decode_base64("SGVsbG8g8J+MjQ==")) # "Hello 🌍" Python's base64 module has dedicated functions for URL-safe Base64 (replaces + with - and / with _):
🌐
GeeksforGeeks
geeksforgeeks.org › python › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python - GeeksforGeeks
August 7, 2024 - First, the strings are converted into byte-like objects and then encoded using the base64 module. The below example shows the implementation of encoding strings isn't base64 characters. ... import base64 sample_string = "GeeksForGeeks ...
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › base64-with-python
Base64 with Python | Binary Encoding Techniques Across Programming Languages
In Python, you can easily encode file content into Base64 and then decode it back. The process involves reading the file’s raw bytes, encoding those bytes, and then writing the resulting Base64 string.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals › oop
Base64 Encoding and Decoding Using Python | Envato Tuts+
April 11, 2022 - The first thing we have to do in order to use Base64 in Python is to import the base64 module: