The difference between Python3 bytes and str

Source: Internet
Author: User

Original: http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3/

The most important new feature of Python 3 is probably a clearer distinction between text and binary data. Text is always Unicode, represented by the STR type, and binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between the two particularly clear. You can't stitch strings and byte packets, or search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa). This is a good thing.

In any case, the line between the string and the byte packet is inevitable, and the following diagram is important, and be sure to keep in mind:


Strings can be encoded into byte packets, while byte packets can be decoded into strings.

>>> ' €20 ' encode (' Utf-8 ')
B ' \xe2\x82\xac20 '
>>> B ' \xe2\x82\xac20 '. Decode (' Utf-8 ')
' €20 '

This is a question to look at: strings are abstract representations of text. Strings consist of characters, which are abstract entities that are independent of any particular binary representation. In manipulating strings, we live in a blissful ignorance. We can segment and fragment strings, and we can stitch and search strings. We don't care how they are expressed internally, and each character in the string is saved in several bytes. We begin to pay attention only when strings are encoded into byte packets (for example, to send them on a channel) or to decode strings from byte packets (reverse operation).

Parameters for incoming encode and decode are encoded (or codec). Encoding is a way of representing abstract characters with binary data. There are many kinds of coding at present. The UTF-8 given above are one of them, and the following is another:

>>> ' €20 ' encode (' iso-8859-15 ')
B ' \xa420 '
>>> B ' \xa420 '. Decode (' iso-8859-15 ')
' € 20 '

Coding is a vital part of this conversion process. Away from the code, Bytes object B ' \xa420 ' is just a bunch of bit bits. Code to give it meaning. With different encodings, the meaning of this heap bit will be very different:
>>> b ' \xa420 ' decode (' windows-1255 ')
' ₪20 '



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.