Code Summary in Python:

Source: Internet
Author: User

Coding Summary in Python: 1 Python uses the encoding of the program file to encode the Chinese string, so the Utf-8, GBK program's recorded Chinese is different encoding 2 can be forced to encode using Unicode encoding, using the u character str = u ' Chinese character ' 3. Utf-8 each Chinese occupies 3 bytes \xe8\x80\x834 GBK each Chinese occupies 2 bytes \xbf\xbc5 encoding Introduction
Encoding Rules
UTF-8 encoding: An English character equals one byte, one Chinese (with traditional) equals three bytes Unicode encoding: one English equals two bytes, and one Chinese (with traditional) equals two bytes.
Verification:
write 1 characters in Notepad to see the size of the file
Gbk:2 Bytes |  Unicode:4 bytes | utf-8:6 bytes
Notepad write 2 Chinese characters
GBK: 4 bytes   |   unicode:6 bytes   Span style= "FONT-SIZE:10.5PT; line-height:1.5 ">|   utf-8:9 bytes
    There seems to be a problem, besides GBK, the other two encodings are completely incompatible with coding rules ... , finally, by viewing the 16-input editor ( ultraedit " View the contents of the file Discovery Notepad for Unicode, utf-8 encoding begins with a character that occupies a space for a Chinese character (2 bytes, 3 bytes
Notepad write 1 kanji to see the size of a file  
gbk:2 bytes  |  unicode: 2 | &NBSP, utf-8: 3 byte
Notepad write 2 Chinese characters
GBK: 4 bytes   |   unicode:4 byte   |   utf-8: 6
remark:
1If you want to get Chinese encoding, you can use Str.encode (' hex ')
str = "Test" <span style= "White-space:pre" ></span>for item in str:  <span style= "White-space:pre" > </span>print item.encode ("hex")  #每个汉子 use 3 bytes

2 bytes (byte): 8-bit (bit)

Code Summary in Python:

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.