🌐
Reddit
reddit.com › r/learnpython › why is encoding bytes to utf-8 called decoding?
r/learnpython on Reddit: Why is encoding bytes to UTF-8 called decoding?
March 30, 2022 -

This always confuses me every single time. I remember it by doing the opposite of what I intuitively think it should be. Perhaps I'm the only one?

bytes.decode - encodes the bytes as UTF-8

str.encode - decodes "removes" the UTF-8 encoding

There is also the abbreviation BADTIE: Bytes Are Decoded, Text Is Encoded. To help to remember it.

Top answer
1 of 4
25
You are indeed confused: bytes.decode - encodes the bytes as UTF-8 No, it decodes a byte sequence to a string, interpreting it as an UTF-8 encoded string. Strings are a sequence of code points (or characters) in Unicode. Unicode and UTF-8 are not the same thing! UTF-8 is the most popular, but still only one of many encoding schemes for Unicode. Take the string ABCÄĀ. This is represented by the unicode code point sequence U+0041 U+0042 U+0043 U+00c4 U+0100. As you see, unicode code points are more than 8 bits, so we need a way to store them in 8-bit byte sequences. One way to do that is to use UTF-8, which turns it into the byte sequence 0x41 0x42 0x43 0xc3 0x84 0xc4 0x80. As another example, UTF-16 would encode this as 0xff 0xfe 0x00 0x41 0x00 0x42 0x00 0x43 0x00 0xc4 0x01 0x00. Notice that except for the 0xff 0xfe BOM (Byte Order Mark) at the beginning, it's simply storing the 16 bit value of the code point in sequence (code points can have more than 16 bits, so it only works this way below 32768, similar to how UTF-8 doesn't change the representation below 128). The BOM is needed to know which of the two bytes represents the most significant bits, as in, is 0x12 0x34 representing 0x1234 or 0x3412. (Some machines store 16 bit values in the first way ("big endian"), others, like x86, in the other ("little endian")) There are also other encoding schemes, for example, if every unicode code point is below 256, you can use latin-1, which will just use the lower 8 bits of the unicode code point. To summarize: bytes.decode decodes a byte sequence into a string, which is an abstract representation of code points. str.encode turns this abstract representation into a byte sequence.
2 of 4
11
Because in Python 3 the strings are in unicode by default, and bytes are an optional encoding, if you want to deal with things on that level.
Printing UTF-8 Jun 16, 2016
r/learnpython
10y ago
The use of open(encoding="utf-8") Nov 2, 2020
r/learnpython
5y ago
Trouble Decoding from UTF-8 Feb 11, 2025
r/AskProgramming
last yr.
Why was string.decode() removed in Python 3? Feb 23, 2018
r/learnpython
8y ago
More results from reddit.com
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › `bytes`: the lesser-known python built-in sequence • and understanding utf-8 encoding
`bytes`: The Lesser-Known Python Built-In Sequence • And Understanding UTF-8 Encoding
July 15, 2025 - The first three bytes represent the first three characters C, a, and f. These are single bytes containing the characters' ASCII codes, which each fit within a single byte. The last two bytes combined represent the last character, é. ... Do you want to try video courses designed and delivered in the same style as these posts? You can get a free trial at The Python Coding Place and you also get access to a members-only forum.
Discussions

python - Encode byte string in utf8 - Stack Overflow
Just use the original string. You need to explain why you can't just use the original string. 2015-01-08T18:10:11.913Z+00:00 ... I have a function which takes a string as an argument, but somehow it checks if the input string is utf8 or unicode. What I have is a compressed byte string. More on stackoverflow.com
🌐 stackoverflow.com
How to convert a string to utf-8 in Python - Stack Overflow
It creates Unicode string in Python 3 (good) but it is a bytestring in Python 2 (bad). Either add from __future__ import unicode_literals at the top or use u'' prefix. Don't use non-ascii characters in bytes literals. To get utf-8 bytes, you could utf8bytes = unicode_text.encode('utf-8') later ... More on stackoverflow.com
🌐 stackoverflow.com
Python: Convert utf-8 string to byte string - Stack Overflow
I have the following function to parse a utf-8 string from a sequence of bytes Note -- 'length_size' is the number of bytes it take to represent the length of the utf-8 string def parse_utf8(self, More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
python how to read bytes type data from file and convert it to utf-8? - Stack Overflow
1 How to convert a string representation of 'bytes' to its corresponding 'utf-8' in Python? More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-a-string-to-utf-8-in-python
Convert a String to Utf-8 in Python - GeeksforGeeks
July 23, 2025 - In this example, the bytes constructor is used with the original string and the encoding 'utf-8'. ... original_string = "Hello, World!" utf8_bytes = bytes(original_string, 'utf-8') print("Original String:", original_string) print("UTF-8 Bytes:", utf8_bytes)
🌐
Finxter
blog.finxter.com › home › learn python blog › converting python bytes to utf-8 strings: 5 best methods
Converting Python Bytes to UTF-8 Strings: 5 Best Methods - Be on the Right Side of Change
February 23, 2024 - The most straightforward method to convert bytes to a UTF-8 string in Python is to use the decode() method available on byte objects. By specifying ‘utf-8’ as the decoding scheme, the bytes are properly converted into a UTF-8 encoded string.
🌐
Programming Idioms
programming-idioms.org › idiom › 304 › encode-string-into-utf-8-bytes › 5650 › python
Encode string into UTF-8 bytes, in Python
Test if bytes are a valid UTF-8 string · Write "Ni Hao" in Chinese to standard output in UTF-8 · Cheatsheets · Issues · Report a bug · × · Python · data = s.encode('utf8') C# byte[] data = Encoding.UTF8.GetBytes(s); Go · data := []byte(s) JS · const data = new TextEncoder().encode(s); Java ·
🌐
Finxter
blog.finxter.com › home › learn python blog › python convert unicode to bytes, ascii, utf-8, raw string
Python Convert Unicode to Bytes, ASCII, UTF-8, Raw String - Be on the Right Side of Change
June 30, 2021 - Due to the fact that UTF-8 encoding ... For this task, both of the above methods are applicable. With encode(), we first get a byte string by applying UTF-8 encoding to the input Unicode string, and then use decode(), which will give us a UTF-8 encoded Unicode string that is already ...
Find elsewhere
🌐
Evanjones
evanjones.ca › python-utf8.html
How to Use UTF-8 with Python (evanjones.ca)
Now, the byte string s will be treated as a sequence of UTF-8 bytes to create the Unicode string u. The next line stores the UTF-8 representation of u in the byte string backToBytes. Thankfully, everything in Python is supposed to treat Unicode strings identically to byte strings.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-convert-strings-to-utf8-bytes
How to Convert Strings to UTF-8 Bytes in Python | Tutorial Reference
The most direct and reliable way to convert a string into UTF-8 bytes is by using the built-in .encode() method available on every Python string object.
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.15.0 documentation
To summarize the previous section: ... to be represented in memory as a set of code units, and code units are then mapped to 8-bit bytes....
🌐
freeCodeCamp
freecodecamp.org › news › python-bytes-to-string-how-to-convert-a-bytestring
Python Bytes to String – How to Convert a Bytestring
April 10, 2023 - Note that the decode() method can ... the decoder should expect more input. You can use the str() constructor in Python to convert a byte string (bytes object) to a string object....
🌐
SSOJet
ssojet.com › dev guides › character encodings › utf-8 in python
UTF-8 in Python | Developer Guides
3 days ago - To avoid errors, consistently encode ... byte data. When working with text files in Python, explicitly specifying the encoding='utf-8' parameter in the open() function is crucial for accurate character handling....
🌐
Reddit
reddit.com › r/learnpython › utf-8 decoding
r/learnpython on Reddit: UTF-8 decoding
June 24, 2024 -

Hi! So I'm trying to decode some utf-8 strings in Python. However some bring up an error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 3: invalid start byte or something along the lines of that. Here's the function: def b_numstring(f):

name_len = int.from_bytes(f.read(4), byteorder='big')

string = f.read(name_len).decode('utf-8')

return string Any help would be appreciated. Thanks!