Python Coding issues

Source: Internet
Author: User

A. The bytes used for each encoding.

Ascii
a:00000010 8 bits of one byte

Unicode a:00000000 00000001 00000010 00000100 32 bits four bytes
Medium: 00000000 00000001 00000010 00000110 32 bit four bytes


Utf-8 a:00100000 8 Bit one byte
Medium: 00000001 00000010 00000110 24 bits Three bytes


GBK a:00000110 8 Bit one byte
Medium: 00000010 00000110 16 bit two bytes
1, the binary system between each encoding, it can not recognize each other, will produce garbled.
2, file storage, transmission, cannot be Unicode (only utf-8 utf-16 gbk,gb2312,asciid, etc.), because Unicode occupies too much space.

Second, the encoding method in Python3:

STR is encoded in memory in Unicode and cannot be stored or transmitted directly.
Bytes Type: Basically the same as STR, but the encoding is different.
For English:
STR: representation: s = ' Alex ' (WYSIWYG)
Encoding method: 010101010 Unicode encoding
Bytes: representation: s = B ' Alex ' (WYSIWYG)
Encoding method: 000101010 Utf-8, GBK and other encoding methods.

For Chinese:
STR: representation: s = ' China '
Encoding method: 010101010 Unicode
Bytes: expression form: s = B ' x\e91\e91\e01\e21\e31\e32 '
Encoding method: 000101010 utf-8 GBK ....

Third, the code. Encode convert Unicode to utf-8 or GBK (expression is str converted to bytes)

S1 = "Alex"
S11 = S1.encode ("Utf-8")
S12 = S1.encode ("GBK")
S2 = "China"
S22 = S2.encode ("Utf-8")
S23 = S2.encode ((' GBK '))
Print (S11,S12,S22,S23)

Python Coding issues

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.