Python rookie promotion----Python3 's bytes and str

Source: Internet
Author: User

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

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.