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 '