Hướng dẫn byte to string python
In this article, we'll take a look at how to convert Bytes to a String in Python. By the end of this article you will have a clear idea of what these types are and how to effectively handle data using them.
Depending on the version of Python you're using, this task will differ. Although Python 2 has reached its end of life, many projects still use it, so we'll include both the Python 2 and Python 3 approaches.
Convert Bytes to String in Python 3
Since Python 3, the old ASCII way of doing things had to go, and Python became completely Unicode.
This means that we lost the explicit unicode type:
To differentiate these strings from good old bytestrings, we're introduced to a new specifier for them - the
Bytestrings in Python 3 are officially called
Convert Bytes to String with decode()
Let's take a look at how we can convert bytes to a String, using the built-in
Passing the encoding format, we've decoded the
Convert Bytes to String with codecs
Alternatively, we can use the built-in
You don't really need to pass in the encoding parameter, though, it is advised to pass it in:
Convert Bytes to String with str()
Finally, you can use the
Make sure to provide the encoding argument to
This brings us to encodings once again. If you specify the wrong encoding, the best case is your program crashing because it can't decode the data. For example, if we tried using the
This is even more important given that Python 3 likes to assume Unicode - so if you are working with files or data sources that use an obscure encoding, make sure to pay extra attention.
Convert Bytes to String in Python 2
In Python 2, a bundle of bytes and a string are practically the same thing - strings are objects consisting of 1-byte long characters, meaning that each character can store 256 values. That's why they are sometimes called bytestrings.
This is great when working with byte data - we just load it into a variable and we are ready to print:
Using Unicode characters in bytestrings does change this behavior a bit though:
Convert Bytes to Unicode (Python 2)
Here, we'll have to use Python 2's
As you can see above, the Unicode string contains
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
You can get the Unicode string by decoding your bytestring. This can be done by constructing a Unicode object, providing the bytestring and a string containing the encoding name as arguments or by calling
Convert Bytes to String Using decode() (Python 2)
You can also use the
Convert Bytes to String Using codecs (Python 2)
Or, using the
Be Mindful of your Encoding
A word of caution here - bytes can be interpreted differently in different encodings. With around 80 different encodings available out of the box, it might not be easy to know if you've got the right one!
The original message was either
As programmers, there are some things we must constantly think about and actively prepare for in order to avoid pitfalls. This holds especially true on the lower levels, where we seldom go when we use a high-level language like Python as our daily driver.
Things like charsets, encodings and binary are there to remind us that our job is to code - to encode our thoughts into working solutions. Thankfully, a lot of this thinking becomes part of our routine after a few rounds at the keyboard.
In this article, we've gone over how to convert bytes to Strings in Python.