Python 3 Description of data conversion for strings

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:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6D/8E/wKiom1Vl_qHQInFcAABRA4eYcjg362.jpg "title=" 01qon7axn1il.png "alt=" Wkiom1vl_qhqinfcaabra4eycjg362.jpg "/>

Diagram, very important.


A string can be encoded into a byte packet, and a byte packet can be decoded into a string.

>>> ' Encode (' utf-8 ') b ' \xe2\x82\xac20 ' >>> B ' \xe2\x82\xac20 '. Decode (' utf-8 ') ' 20 '

This is the problem: strings are an abstract representation of text. Strings are composed of characters, and the characters are abstract entities that are not related to any particular binary representation. We live in the ignorance of happiness while manipulating strings. We can split and shard strings, and we can stitch and search strings. We don't care how they are represented internally, and each character in the string is saved in a few bytes. It is only when the strings are encoded into a byte package (for example, to send them on a channel) or decoded from a byte packet (reverse operation) that we begin to pay attention to this.


The parameters passed in encode and decode are encoded (or codec). Encoding is a way of representing abstract characters in binary data. There are many kinds of coding at present. The UTF-8 given above is one of them, and here is another:

>>> ' Encode (' iso-8859-15 ') b ' \xa420 ' >>> B ' \xa420 '. Decode (' iso-8859-15 ') ' 20 '

Coding is a critical part of this transformation process. Away from the code, Bytes object B ' \xa420 ' is just a bunch of bits. The encoding gives it meaning. With different encodings, the meaning of this heap of bits can be very different:

>>> b ' \xa420 '. Decode (' windows-1255 ') ' 20 '


Extended reading: The unicode--code of the Dumb School

This article is from the "Professor" blog, please be sure to keep this source http://professor.blog.51cto.com/996189/1655816

Python 3 Description of data conversion for strings

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.