How to Convert Bytes to String in Python
In this article, we’ll take a look at how to convert Bytes to a String in Python.
Convert Bytes to String with decode()
>>> b = b"Lets take a \xF0\x9F\x9B\x80!" # Let's check the type >>> type(b) <class 'bytes'> # Now, let's decode/convert them into a string >>> s = b.decode('UTF-8') >>> s "Lets take a 🛀!"
Convert Bytes to String with codecs
>>> import codecs >>> b = b'Lets grab a \xF0\x9F\x9B\x80!' >>> codecs.decode(b, 'UTF-8') "Lets take a 🛀!!"
Convert Bytes to String with str()
>>> b = b'Lets grab a \xF0\x9F\x9B\x80!' >>> str(b, 'UTF-8') "Lets take a 🛀!!"
Subscribe
Login
Please login to comment
0 Discussion