Str and bytes are distinguished in Python3, bytes represents a binary byte, str represents a text string, text is always Unicode, is represented by the STR type, and binary data is represented by a bytes type. Python 3 does not mix str and bytes in any implicit way, and the two require a coded conversion when mixed
The line between a string and a byte packet is inevitable, the string can be encoded into a byte packet, and the byte packet can be decoded into a string, the following diagram is very important, be sure to keep in mind:
Example:
>>> ' €20 '. Encode (' Utf-8 ') # encode (parameter) indicates that the format Type B ' \xe2\x82\xac20 ' # before being converted is in binary format
' €20 '
str->bytes: encode encoding; encoding is the conversion of a string into a bytecode, involving the internal representation of a string.
bytes->str decoding is the conversion of bytecode into strings, and the bit bits are displayed as characters.
In purely manipulating string-type data, there is no need to consider strings and binary conversions, only when strings are encoded into byte packets (for example, by using the socket network for development, sending them on the channel; Python3 are all binary transmissions) or decoding strings from byte packets (at programming time, We start to focus on this if the program can only handle text that cannot handle binary and needs to be converted. In the computer, the video is stored in binary format, the text is stored in a string format (it can actually be forced to be stored as a binary).
Python base python3 str and bytes