You can't decode a unicode, and you can't encode a str. Try doing it the other way around.

Answer from Ignacio Vazquez-Abrams on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-encode-decode
Python String Encode and Decode: Complete Guide | DigitalOcean
Master Python string encode() and decode() methods. Learn UTF-8, ASCII, Unicode handling, error handling modes, and practical encoding/decoding examples.
🌐
Python
docs.python.org › 3 › library › codecs.html
codecs — Codec registry and base classes
Codec authors also need to define how the codec will handle encoding and decoding errors. To simplify and standardize error handling, codecs may implement different error handling schemes by accepting the errors string argument: >>> 'German ß, ♬'.encode(encoding='ascii', errors='backslashreplace') b'German \\xdf, \\u266c' >>> 'German ß, ♬'.encode(encoding='ascii', errors='xmlcharrefreplace') b'German ß, ♬' The following error handlers can be used with all Python Standard Encodings codecs:
Top answer
1 of 4
87

You can't decode a unicode, and you can't encode a str. Try doing it the other way around.

2 of 4
61

Guessing at all the things omitted from the original question, but, assuming Python 2.x the key is to read the error messages carefully: in particular where you call 'encode' but the message says 'decode' and vice versa, but also the types of the values included in the messages.

In the first example string is of type unicode and you attempted to decode it which is an operation converting a byte string to unicode. Python helpfully attempted to convert the unicode value to str using the default 'ascii' encoding but since your string contained a non-ascii character you got the error which says that Python was unable to encode a unicode value. Here's an example which shows the type of the input string:

>>> u"\xa0".decode("ascii", "ignore")

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    u"\xa0".decode("ascii", "ignore")
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 0: ordinal not in range(128)

In the second case you do the reverse attempting to encode a byte string. Encoding is an operation that converts unicode to a byte string so Python helpfully attempts to convert your byte string to unicode first and, since you didn't give it an ascii string the default ascii decoder fails:

>>> "\xc2".encode("ascii", "ignore")

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    "\xc2".encode("ascii", "ignore")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-strings-decode-method
Python Strings decode() method - GeeksforGeeks
May 11, 2026 - Base64 encoding only converts data into a different format and is easily reversible, so it does not provide security. It is later decoded to verify the login.
🌐
Mimo
mimo.org › glossary › python › string-decode
Python string decode(): Syntax, Usage, and Examples
To decode a byte object, call .decode() on it and pass in the encoding type. UTF-8 is the most common encoding used on the web and in most modern applications. The method also accepts an errors argument that controls how decoding errors are handled. ... Become a Python developer.
🌐
Reddit
reddit.com › r/learnpython › decode and encode with python
r/learnpython on Reddit: Decode and Encode with Python
July 28, 2022 -

I am getting a byte base64 encoded data and I have to decode it but it is showing an error on the last line.

Error - UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 1: ordinal not in range(128)

base64_bytes = base64_message.encode('ascii')
print(base64_bytes)
#decode
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii')
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
Python Examples Python Compiler ... Python Study Plan Python Interview Q&A Python Training ... The encode() method encodes the string, using the specified encoding....
🌐
Medium
medium.com › @vivekmcm1 › understanding-pythons-encode-and-decode-with-real-world-examples-5d2080d66f01
Understanding Python’s encode() and decode() with Real-World Examples | by Vivek | Medium
August 30, 2025 - ... text = "Hello World" byte_text ... because ASCII cannot represent it. ... Decoding is the reverse process: converting bytes back into a string....
🌐
YouTube
m.youtube.com › watch
Encode and Decode Strings - Leetcode 271 - Python
Share your videos with friends, family, and the world
Published: July 14, 2021
🌐
DEV Community
dev.to › hyperkai › encode-decode-in-python-18l7
encode & decode in Python - DEV Community
October 18, 2025 - It controls encoding or decoding error with the error handlers, 'strict', 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', etc.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-encode-and-decode-a-message-using-python
How To Encode And Decode A Message using Python? - GeeksforGeeks
May 11, 2023 - In this article, we will take forward the idea of encryption and decryption and draft a python program. In this article, we will be given a single-line message as input it is either encoded or decoded as per requirement and the resultant message is printed as output.
🌐
YouTube
youtube.com › shorts › zwUjHW8Exyc
Encode and Decode Strings - Leetcode 271 - YouTube
🚀 https://neetcode.io/ - Get lifetime access to every course I ever create!Checkout my second Channel: https://www.youtube.com/@NeetCodeIO🥷 Discord: https
Published: June 23, 2024
🌐
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - Encoding and decoding is the process of going from one to the other: ... In .encode() and .decode(), the encoding parameter is "utf-8" by default, though it’s generally safer and more unambiguous to specify it:
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
$ echo '{"json":"obj"}' | python -m json { "json": "obj" } $ echo '{1.2:3.4}' | python -m json Expecting property name enclosed in double quotes: line 1 column 2 (char 1) See Command-line interface for detailed documentation. ... JSON is a subset of YAML 1.2. The JSON produced by this module’s default settings (in particular, the default separators value) is also a subset of YAML 1.0 and 1.1. This module can thus also be used as a YAML serializer. ... This module’s encoders and decoders preserve input and output order by default.
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.7 documentation
Unicode data is usually converted to a particular encoding before it gets written to disk or sent over a socket. It’s possible to do all the work yourself: open a file, read an 8-bit bytes object from it, and convert the bytes with bytes.decode(encoding).
🌐
Open Source For You
opensourceforu.com › home › audience › developers › encoding and decoding in python for managing data
Encoding and Decoding in Python for Managing Data
January 17, 2024 - Or, you may get a ValueError if you try to decode a string data that is not a valid Base64 format. And you may get a TypeError if you try to encode or decode a data type that is not a string, such as an integer or a list. Python uses UTF-8 encoding as the default method for the encode() function.
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Without using 'rb', Python would assume we are reading a text file. We then use the read() method to get all the data in the file into the binary_file_data variable. Similar to how we treated strings, we Base64 encoded the bytes with base64.b64encode and then used the decode('utf-8') on base64_encoded_data to get the Base64 encoded data using human-readable characters.
🌐
Python
docs.python.org › 3 › library › base64.html
base64 — Base16, Base32, Base64, Base85 Data Encodings
Encode bytes-like object s using the URL- and filesystem-safe alphabet, which substitutes - instead of + and _ instead of / in the standard Base64 alphabet, and return the encoded bytes.
🌐
AskPython
askpython.com › python › string › python-encode-and-decode-functions
Python encode() and decode() Functions - AskPython
February 16, 2023 - Similar to those of encode(), the decoding parameter decides the type of encoding from which the byte sequence is decoded.
🌐
Tutorialspoint
tutorialspoint.com › python › string_decode.htm
Python String decode() Method
The encoded string is: b'Hello! Welcome to Tutorialspoint.' The decoded string is: Hello! Welcome to Tutorialspoint. The python string decode() method that takes 'utf_32' as its encoding has a variable length encoding done. If the error is specified as 'replace', then it is replaced with a replacement marker.