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 them particularly clear. You cannot stitch strings and byte packets , Search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa). It's a good thing.
In any case, the line between the string and the byte packet is inevitable, and the following diagram is important to keep in mind:
A string can be encoded into a byte packet, and a byte packet can be decoded into a string.
>>> "Hello". Encode ("Utf-8") #encode将字符串变成字节b ' \xe4\xbd\xa0\xe5\xa5\xbd ' >>> B ' \xe4\xbd\xa0\xe5\ Xa5\xbd '. Decode (' Utf-8 ') #decode将字节变成字符串 ' Hello '
Of course, encoding can also be used in other encoding formats, such as GBK
Previous: Python rookie promotion----python3.x and python2.x Difference
Next: Python rookie promotion----The difference between the repr () and the STR () function
If you have any questions, welcome to my public question ~
Python rookie promotion----Python3 's bytes and str