![]() This stores strings as a series of code points, rather than bytes. Here, we'll have to use Python 2's Unicode type, which is assumed and automatically used in Python 3. 'Lets grab a \xf0\x9f\x8d\x95!' # Where has the pizza gone to? > len(s)ġ7 # Shouldn't that be 15? Convert Bytes to Unicode (Python 2) Using Unicode characters in bytestrings does change this behavior a bit though: > s = "Let's grab a □!" > s This is great when working with byte data - we just load it into a variable and we are ready to print: > s = "Hello world!" > s That's why they are sometimes called bytestrings. 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. 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. For example, if we tried using the str() function with UTF-16, we'd be greeted with: If you specify the wrong encoding, the best case is your program crashing because it can't decode the data. Make sure to provide the encoding argument to str() though, otherwise you might get some unexpected results: > str(b) "Let's grab a □!" Convert Bytes to String with str()įinally, you can use the str() function, which accepts various values and converts them into strings: > b = b'Lets grab a \xf0\x9f\x8d\x95!' > str(b, 'UTF-8') You don't really need to pass in the encoding parameter, though, it is advised to pass it in: > code(b) Convert Bytes to String with codecsĪlternatively, we can use the built-in codecs module for this purpose as well: > import codecs Passing the encoding format, we've decoded the bytes object into a string and printed it. # Now, let' s decode/ convert them into a string > b = b"Lets grab a \xf0\x9f\x8d\x95!" # Let's check the type > type(b) ![]() Let's take a look at how we can convert bytes to a String, using the built-in decode() method for the bytes class: Another bytes-like object added in 2.6 is the bytearray - similar to bytes, but mutable. This was added in Python 2.6, but it served no real purpose other than to prepare for Python 3 as all strings were bytestrings in 2.6.īytestrings in Python 3 are officially called bytes, an immutable sequence of integers in the range 0 <= x < 256. To differentiate these strings from good old bytestrings, we're introduced to a new specifier for them - the b"string". This means that we lost the explicit unicode type: u"string" - every string is a u"string"! ![]() Since Python 3, the old ASCII way of doing things had to go, and Python became completely Unicode. 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. 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.ĭepending on the version of Python you're using, this task will differ. Let us look at the above concepts using a simple example.In this article, we'll take a look at how to convert Bytes to a String in Python. Inserts a backslash escape sequence ( \uNNNN) instead of un-encodable Unicode characters. Replaces all un-encodable Unicode characters with a question mark ( ?) Ignores the un-encodable Unicode from the result. There are various types of errors, some of which are mentioned below: Type of Errorĭefault behavior which raises UnicodeDecodeError on failure. This is actually not human-readable and is only represented as the original string for readability, prefixed with a b, to denote that it is not a string, but a sequence of bytes. This means that the string is converted to a stream of bytes, which is how it is stored on any computer. Although there is not much of a difference, you can observe that the string is prefixed with a b. NOTE: As you can observe, we have encoded the input string in the UTF-8 format. Original string: This is a simple sentence.Įncoded string: b'This is a simple sentence.'
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |