The coding difference between Python2 and Python3

Source: Internet
Author: User
1. Str and bytes 1.1 str is text, Bytes is byte

Text is encoded (utf-8,gbk,gb2312, etc.)
BYTE is not encoded

Text encoding refers to how the character uses bytes to represent the organization, and Linux defaults to using UTF-8
Bytes 2 can be defined by a B prefix . Python2 and Python3 Coding 2.1 Python2

In Python2, the bytes and STR types are not distinguished, and in Python3 bytes and Str are differentiated, and all operations of STR bytes Support

Python2

Python3

In addition to encode, the STR operation has a corresponding bytes version, but the incoming parameter must be a conversion ——-encoding between bytes 2.2 Python3 2.2.1 and str

bytes by str through the Encode method.
Str can also be transformed by bytes through the Decode method .

GBK is a double-byte, UTF-8 flexible encoding, 1-byte, 2-byte, 3-byte, 4-byte, maximum support 6-byte length, most of the Chinese is 3 bytes

In simple terms:
in Py3, str is equivalent to the ToString () method in Java

When a character is encoded in a different form, the corresponding byte is not the same
Like what:
1. Encode converted word Throttling bytes is not a string.

Bytes is not a string str. In Python3, Bytes is a byte stream bytes object, and the string is a string str object. 2. The same is true of the string encode (), why some of the results are letters, and some hexadecimal \x

The actual function of encoding is to convert a string into a byte form corresponding to the encoding.

Note: when running the interface output bytes, it takes the principle that each byte is read in terms of the ASCII code, and if ASCII-compatible characters (special characters, letters and numbers, except control characters) are used, the byte is expressed in ASCII code, Otherwise, it is represented by the hexadecimal \x so-and-so.

For example, "ABC China" after Utf-8 encoding (encode () by default in utf-8 form), the resulting bytes are as follows:

When the interface is output bytes, each byte is read sequentially and compared with the ASCII encoding. Because the first three bytes can be found in the ASCII encoding table, the corresponding characters are displayed directly.

For the first byte in the byte hexadecimal byte e4b8ad of the Chinese character "E4", the corresponding decimal is 228, not within the ASCII encoding range, so the hexadecimal 3 is output directly when the interface is output . How do you define the bytes variable?

Direct use of B ' string ' to represent the bytes object, which can be a character in ASCII range, or a hexadecimal string, but not Chinese

Similarly, when the interface is output, it looks for each character sequentially in ASCII. 4. The difference between English coding and Chinese coding

5. The Python program editing Interface Unicode code u ' A ' is a string or byte code. What does Unicode code mean?

U ' A ' is not a byte code, it is a string. In other words, in Python3, U ' A ' is the Str object .

The expression in the following figure, the first "\u" represents the Unicode encoding, and the second "\" is an antisense character
6. If it represents a string, then how to convert to a Unicode code. Specifies the Unicode method with encode.

7. Len ()

Bytes and str objects except the decode () and Encode () methods are different, other str methods bytes generally have

2.2 using Utf-8 for conversion

When manipulating strings, we often encounter the reciprocal conversion of STR and bytes. To avoid garbled problems, you should always insist on using UTF-8 encoding to convert str and bytes

Reference
Http://www.jianshu.com/p/6cbb2b14cda9

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.