🌐
Python Central
pythoncentral.io › encoding-and-decoding-strings-in-python-3-x
Encoding and Decoding Strings (in Python 3.x) | Python Central
December 29, 2021 - Encoding and decoding strings in Python 2.x was somewhat of a chore, as you might have read in another article. Thankfully, turning 8-bit strings into unicode strings and vice-versa, and all the methods in between the two is forgotten in Python 3.x.
🌐
GeeksforGeeks
geeksforgeeks.org › python-strings-decode-method
Python Strings decode() method - GeeksforGeeks
April 5, 2025 - It interprets bytes according to a specified character encoding to produce a readable string. ... # Define a bytes object bytes_obj = b'Hello' # Decode bytes object to string string_obj = bytes_obj.decode('utf-8') print(string_obj) # Output: Hello ... # Define a bytes object bytes_data = b'Hello, Python!'
Discussions

Python 3 Decoding Strings - Stack Overflow
Last point of note is that I'm working with Python 3 so raw is unicode, and thus is already decoded. Given that, what exactly do I need to do to "decode" the "\x94" characters? ... If you already have a Unicode string, your website scraping used the wrong encoding to decode the data to Unicode. More on stackoverflow.com
🌐 stackoverflow.com
Python 3 - Encode/Decode vs Bytes/Str - Stack Overflow
I've read some good posts, that made it all much clearer, however I see there are 2 methods on python 3, that handle encoding and decoding, and I'm not sure which one to use. So the idea in python 3 is, that every string is unicode, and can be encoded and stored in bytes, or decoded back into ... More on stackoverflow.com
🌐 stackoverflow.com
python - string encoding and decoding? - Stack Overflow
Thank you for posting the link. 2014-06-30T02:46:38.943Z+00:00 ... @rogueprocess By your description, Python has it right. A str is really a byte sequence (in Python 2.x). You decode a UTF-8 byte sequence into a Unicode string, and you encode a Unicode string into a UTF-8 byte sequence. More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
CodeSignal
codesignal.com › learn › courses › string-manipulation-for-python-coders › lessons › navigating-the-universe-of-python-unicode-encoding-and-decoding-strings-explained
Unicode, Encoding, and Decoding Strings Explained
str1 = "Hello from Mars!" b = str1.encode('UTF-16') # Encoding the string in UTF-16 print("UTF-16 Encoded string: ", b) # Prints: UTF-16 Encoded string: b'\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00f\x00r\x00o\x00m\x00 \x00M\x00a\x00r\x00s\x00!\x00' ... str2 = b.decode('UTF-16') # Decoding the string print("Decoded string: ", str2) # Prints: 'Hello from Mars!' Unicode supports multiple scripts, thus allowing Python to efficiently handle non-English characters.
🌐
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 - Here’s the fundamental concept: – **Encoding**: Convert Unicode strings → bytes (for storage/transmission) – **Decoding**: Convert bytes → Unicode strings (for processing/display) # The basic dance text = "Hello, 世界!
🌐
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)
🌐
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.
🌐
Honeybadger
honeybadger.io › blog › python-character-encoding
Python developer's guide to character encoding - Honeybadger Developer Blog
March 6, 2023 - As stated previously, Python 3 has two data types: strings and bytes. The process of moving between strings and bytes is known as encoding and decoding in Python.
🌐
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - If the word “text” is found in the Content-Type header, and no other encoding is specified, then requests will use ISO-8859-1. The complete list of accepted encodings is buried way down in the documentation for the codecs module, which is part of Python’s Standard Library. There’s one more useful recognized encoding to be aware of, which is "unicode-escape". If you have a decoded str and want to quickly get a representation of its escaped Unicode literal, then you can specify this encoding in .encode():
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used. ... These examples uses ascii encoding, and a character that cannot be encoded, showing the result with different errors:
🌐
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. Python comes with roughly 100 different encodings; see the Python Library Reference at Standard Encodings for a list. Some encodings have multiple names; for example, 'latin-1', 'iso_8859_1' and '8859’ are all synonyms for the same encoding.
🌐
Tutorialspoint
tutorialspoint.com › python › string_decode.htm
Python String decode() Method
The python string decode() method that takes 'utf_16' as its encoding has a variable length encoding done. If the error is specified as 'strict', then the encoding errors raise a UnicodeError. In the following example, a string "Hello! Welcome to Tutorialspoint." is created and it is encoded using the encode() function.
🌐
CodeRivers
coderivers.org › blog › decode-python-string
Decoding Python Strings: A Comprehensive Guide - CodeRivers
February 22, 2026 - Python 2: In Python 2, there are two types related to text: str (which is actually a byte string) and unicode (the true string type). To convert a str (byte string) to a unicode string, you use the decode() method. Python 3: In Python 3, the situation is reversed.
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.

🌐
CodeRivers
coderivers.org › blog › encode-a-string-python
Encoding Strings in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - The str type in Python 3 is a Unicode string. When encoding a str to bytes, you are essentially converting these Unicode code points into a specific byte representation according to the chosen encoding scheme. In Python, you can use the encode() method on a string to convert it into bytes.
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
It returns an utf-8 encoded version of the string. In case of failure, it raises a UnicodeDecodeError exception. ... # unicode string string = 'pythön!' # print string print('The string is:', string) # default encoding to utf-8
🌐
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 ... character is ignored because ASCII cannot represent it. ... Decoding is the reverse process: converting bytes back into a string....
🌐
Python Module of the Week
pymotw.com › 3 › codecs
codecs — String Encoding and Decoding — PyMOTW 3
December 28, 2016 - This example uses PassThrough to show that the data is encoded before being sent, and the response is decoded after it is received in the client. $ python3 codecs_socket.py Sending : 'français' Writing : b'fran\xc3\xa7ais' Reading : b'fran\xc3\xa7ais' Reading : b'' Received: 'français'
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)