First find the encoding of the string and then decode it... to do this you will need to make a byte string by adding the letter 'b' to the front of the original string.

Try this:

import chardet

s = "Aur\xc3\xa9lien"
bs = b"Aur\xc3\xa9lien"

encoding = chardet.detect(bs)["encoding"]

str = s.encode(encoding).decode("utf-8")

print(str)

If you are reading the text from a file you can detect the encoding using the magic lib, see here: https://stackoverflow.com/a/16203777/1544937

Answer from jgphilpott on Stack Overflow
🌐
Python
docs.python.org › 3 › builtins › stdtypes.html
Built-in Types — Python 3.14.7 documentation
When formatting a number (int, float, complex, decimal.Decimal and subclasses) with the n type (ex: '{:n}'.format(1234)), the function temporarily sets the LC_CTYPE locale to the LC_NUMERIC locale to decode decimal_point and thousands_sep fields of localeconv() if they are non-ASCII or longer than 1 byte, and the LC_NUMERIC locale is different than the LC_CTYPE locale.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-strings-decode-method
Python Strings decode() method - GeeksforGeeks
May 11, 2026 - Encoding converts a string into bytes and decode() brings it back to its original form. ... t = "Hello, Python!" e_t = t.encode('utf-8') print("Encoded:", e_t) d_t = e_t.decode('utf-8') print("Decoded:", d_t)
Discussions

Why was string.decode() removed in Python 3?
Python 3 has a much clearer separation between text (str type) and bytes (bytes type). Encoding turns text into bytes, and decoding turns bytes into text. Hence, only bytes has a .decode() method, and only str has a .encode() method. Some of the codecs in Python 2 didn't really fit this pattern; for example, str.decode('hex') basically takes in a series of characters that represent hexadecimal values, and returns some bytes with those hexadecimal values. This behavior doesn't fit into what I said above, so that's why in Python 3 you can only find it in the codecs module, which contains several things that aren't really proper text encodings (where encoding means text -> bytes and decoding means bytes -> text). However, I recommend avoiding these when possible because they are confusing. The binascii module has a function that does exactly what str.decode('hex') used to do: binascii.unhexlify() . This is what I would recommend using since it's much clearer about what it does (including better documented). (EDIT: Actually, u/K900_ 's suggested method (bytes.fromhex()) is maybe even clearer than this one... I would go with that instead. (I didn't realize it existed.)) More on reddit.com
🌐 r/learnpython
7
4
February 23, 2018
python - How to decode a text in python3? - Stack Overflow
The problem is that the Python string has some characters as binary data, not interpreted as unicode code points (which it is an hidden/not very well know feature of Python [and most programmers should never see it]). 2021-02-05T10:38:21.543Z+00:00 ... You have UTF-8 decoded as latin-1, so ... More on stackoverflow.com
🌐 stackoverflow.com
Python 3 - Encode/Decode vs Bytes/Str - Stack Overflow
I am new to python3, coming from python2, and I am a bit confused with unicode fundamentals. I've read some good posts, that made it all much clearer, however I see there are 2 methods on python 3,... More on stackoverflow.com
🌐 stackoverflow.com
beautifulsoup - Is there a way to decode a specific string in Python? - Stack Overflow
I'm using Beautiful Soup to scrape Reddit. After scraping, I get some amount of encoding in the final text after finding the paragraph tag in the HTMl code and taking it's text. # finds all paragra... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 › why was string.decode() removed in python 3?
r/learnpython on Reddit: Why was string.decode() removed in Python 3?
February 23, 2018 -

I'm working on a project where I'm trying to get someone's old Python 2 code updated to work in Python 3, and one of the functions they used was "mystring.decode('hex'). Now I'm trying to figure out how to replicate this functionality in Python 3 and I'm having a hell of a time wrestling with it. Why did Python ditch this?

🌐
Tutorialspoint
tutorialspoint.com › python › string_decode.htm
Python String decode() Method
The python string decode() method decodes the string using the codec registered for its encoding. The encoded string can be decoded and the original string can be obtained with the help of this function.
🌐
Python
docs.python.org › 3 › library › codecs.html
codecs — Codec registry and base classes
This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and error handling lookup process. Most standard codecs are text encodings, which encode text to bytes (and decode bytes to text), but there are also codecs provided that encode text to text, and bytes to bytes.
Find elsewhere
Top answer
1 of 3
74

Neither is better than the other, they do exactly the same thing. However, using .encode() and .decode() is the more common way to do it. It is also compatible with Python 2.

2 of 3
19

To add to Lennart Regebro's answer There is even the third way that can be used:

encoded3 = str.encode(original, 'utf-8')
print(encoded3)

Anyway, it is actually exactly the same as the first approach. It may also look that the second way is a syntactic sugar for the third approach.


A programming language is a means to express abstract ideas formally, to be executed by the machine. A programming language is considered good if it contains constructs that one needs. Python is a hybrid language -- i.e. more natural and more versatile than pure OO or pure procedural languages. Sometimes functions are more appropriate than the object methods, sometimes the reverse is true. It depends on mental picture of the solved problem.

Anyway, the feature mentioned in the question is probably a by-product of the language implementation/design. In my opinion, this is a nice example that show the alternative thinking about technically the same thing.

In other words, calling an object method means thinking in terms "let the object gives me the wanted result". Calling a function as the alternative means "let the outer code processes the passed argument and extracts the wanted value".

The first approach emphasizes the ability of the object to do the task on its own, the second approach emphasizes the ability of an separate algoritm to extract the data. Sometimes, the separate code may be that much special that it is not wise to add it as a general method to the class of the object.

🌐
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.
🌐
Medium
towardsdev.com › python-string-encode-decode-5f159294cd33
Python encode() and decode(). When we work with String sometimes we… | by Sunil Kumar | Towards Dev
August 1, 2022 - Encoding is a way to convert a string to a bytes object. The decoding is a way to convert a bytes object to a string. Python provides encode() and decode() functions to perform Encoding and decoding respectively .
🌐
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')
🌐
GitHub
github.com › samiam1086 › python-tools › blob › main › decode.py
python-tools/decode.py at main · spextat0r/python-tools
return codext.decode(inp, "base91") · except: · return "Not base91" · · #base85Ascii decode function · def base85Ascii(strToDecode: str): · try: · strToDecode = strToDecode.encode() · strToDecode = base64.a85decode(strToDecode) ·
Author: spextat0r
🌐
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).
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)
🌐
The New Stack
thenewstack.io › home › decode any python code with this 5-step method
Decode Any Python Code With This 5-Step Method - The New Stack
July 7, 2025 - Understanding where the program begins helps you trace the flow from start to finish. This means when you run the script directly (python script.py), Python runs the code inside this block.
🌐
Decode Labs
decodelabs.tech
Decode Labs - Global Internship Opportunities | decodelabs.tech
Decode Labs helps learners worldwide gain practical project experience, guided mentorship, and verified outcomes that hiring teams value. ... Most popular tracks chosen by learners. ... Learn AI fundamentals, machine learning, and neural networks. Apply Now ... Master frontend and backend technologies for complete web solutions. Apply Now ... Learn Python ...
🌐
W3Schools
w3schools.com › python › ref_os_fsdecode.asp
Python os.fsdecode() Method
This method decodes the filename from the filesystem encoding with 'surrogateescape' error handler, or 'strict' on Windows.