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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-strings-decode-method
Python Strings decode() method - GeeksforGeeks
May 11, 2026 - decode() method is used to convert encoded text back into its original string format. It works as the opposite of encode() method, which converts a string into a specific encoding format.
🌐
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.
🌐
Mimo
mimo.org › glossary › python › string-decode
Python string decode(): Syntax, Usage, and Examples
The decode() method in Python is used to convert byte data into a Unicode string. In Python 3, strings are Unicode by default, so decode() applies specifically to bytes objects.
🌐
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 documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.7 documentation
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte >>> b'\x80abc'.decode("utf-8", "replace") '\ufffdabc' >>> b'\x80abc'.decode("utf-8", "backslashreplace") '\\x80abc' >>> b'\x80abc'.decode("utf-8", "ignore") 'abc' Encodings are specified as strings containing the encoding’s name.
🌐
AskPython
askpython.com › python › string › python-encode-and-decode-functions
Python encode() and decode() Functions - AskPython
February 16, 2023 - The second one is correct since the encoding and decoding formats are the same. a = 'This is a bit möre cömplex sentence.' print('Original string:', a) # Encoding in UTF-8 encoded_bytes = a.encode('utf-8', 'replace') # Trying to decode via ASCII, which is incorrect decoded_incorrect = encoded_bytes.decode('ascii', 'replace') decoded_correct = encoded_bytes.decode('utf-8', 'replace') print('Incorrectly Decoded string:', decoded_incorrect) print('Correctly Decoded string:', decoded_correct)
Find elsewhere
🌐
Linux Hint
linuxhint.com › python-string-decode-method
Linux Hint – Linux Hint
April 10, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
Using the string encode() method, you can convert unicode strings into any encodings supported by Python.
🌐
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....
🌐
Python
docs.python.org › 3 › library › codecs.html
codecs — Codec registry and base classes
Given a str string of up to 256 characters representing a decoding table, returns either a compact internal mapping object EncodingMap or a dictionary mapping character ordinals to byte values. Raises a TypeError on invalid input. The full details for each codec can also be looked up directly: ... Looks up the codec info in the Python codec registry and returns a CodecInfo object as defined below.
🌐
DataScience Made Simple
datasciencemadesimple.com › home › encode and decode string in python – encode() & decode() function
Encode and Decode string in python - Encode() & Decode() function - DataScience Made Simple
February 4, 2023 - # example of decoding the string in python string_decoded=string_encoded.decode('base64','strict'); print "Decoded string is :"+ string_decoded;
🌐
Python Module of the Week
pymotw.com › 2 › codecs
codecs – String encoding and decoding - Python Module of the Week
$ python codecs_encodings.py Raw : u'pi: \u03c0' UTF-8 : 70 69 3a 20 cf 80 UTF-16: fffe 7000 6900 3a00 2000 c003 · Given a sequence of encoded bytes as a str instance, the decode() method translates them to code points and returns the sequence as a unicode instance.
🌐
Educative
educative.io › answers › how-to-decode-an-encoded-string-in-python
How to decode an encoded string in Python
Once we know the encoding format, we can use Python's built-in module codecs to decode the string.
🌐
MindStick
mindstick.com › interview › 34374 › encode-and-decode-string-function-in-python
encode and decode string function in python? – MindStick
September 17, 2025 - Converts bytes back to string. decoded = encoded.decode("utf-8") # bytes → str print(decoded) # "Hello नमस्ते" import base64 text = "Python Encoding" # Encode string to Base64 encoded = base64.b64encode(text.encode("utf-8")) print(encoded) # b'UHl0aG9uIEVuY29kaW5n' # Decode Base64 back to string decoded = base64.b64decode(encoded).decode("utf-8") print(decoded) # Python 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)
🌐
Zenva
gamedevacademy.org › home › python › python string decoding tutorial – complete guide
Python String Decoding Tutorial - Complete Guide - GameDev Academy
December 12, 2023 - In data handling, you often encounter encoded strings that require decoding. This can be when reading files, working with web APIs, or handling data received over a network, to name a few scenarios. Python’s decoding function allows you to interpret these encoded strings in a manageable form.
🌐
MangoHost
mangohost.net › mangohost blog › python string encode and decode – handling text and bytes
Python String Encode and Decode – Handling Text and Bytes
July 31, 2025 - import time import chardet def benchmark_encodings(): with open('large_file.log', 'rb') as f: raw_data = f.read() # UTF-8 decode start = time.time() text1 = raw_data.decode('utf-8') utf8_time = time.time() - start # Chardet detection + decode start = time.time() detected = chardet.detect(raw_data) text2 = raw_data.decode(detected['encoding']) chardet_time = time.time() - start print(f"UTF-8 direct: {utf8_time:.2f}s") print(f"Chardet + decode: {chardet_time:.2f}s") print(f"Chardet overhead: {(chardet_time/utf8_time - 1)*100:.1f}%") # Typical results: # UTF-8 direct: 0.15s # Chardet + decode: 2.