Python third-party Library series 15--coded library __ Code

Source: Internet
Author: User
Tags chr ord

First Picture:

We know: 1 bytes = 8 bits


Since Python was born earlier than the Unicode standard, the earliest Python only supports ASCII encoding, and the normal string ' ABC ' is ASCII-encoded inside python. Python provides the Ord () and Chr () functions that convert letters and corresponding numbers to each other:

>>> Ord (' a ')
>>> chr ("
a")

Python later added support for Unicode, using Unicode as a string with U ' ... ' For example:

>>> print u ' Chinese '
Chinese
>>> u ' \u4e2d '

Write U ' medium ' and U ' \u4e2d ' is the same, \u is followed by hexadecimal Unicode code. Therefore, U ' A ' and U ' \u0041 ' are the same.

How the two strings are converted to each other. The string ' xxx ', while ASCII-encoded, can also be seen as UTF-8 encoding, while U ' xxx ' is only Unicode encoded.

convert u ' xxx ' to UTF-8 encoded ' xxx ' using encode (' Utf-8 ') method:

>>> u ' abc ' Encode (' utf-8 ')
' abc '
>>> u ' Chinese '. Encode (' utf-8 ')
' \xe4\xb8\xad\xe6\x96\ x87 '

The UTF-8 value is equal to the Unicode value (but occupies different storage space) after the conversion of the English character, and the 1 Unicode characters converted into 3 UTF-8 characters after the Chinese character conversion, and the \xe4 you see is one of the bytes, because its value is 228, There is no corresponding letter to display, so the byte value is displayed in hexadecimal. The Len () function can return the length of the string:

>>> len (U ' abc ')
3
>>> len (' abc ')
3
>>> len (U ' Chinese ')
2
>> > Len (' \xe4\xb8\xad\xe6\x96\x87 ')
6

Conversely, convert the UTF-8 encoded string ' xxx ' to a unicode string u ' xxx ' using the decode (' Utf-8 ') method:

>>> ' abc '. Decode (' Utf-8 ')
u ' abc '
>>> ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' Utf-8 ')
u ' \u4e2d\u6587 '
>>> print ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' utf-8 ')
Chinese

Since the Python source code is also a text file, when your source code contains Chinese, it is important to specify that you want to save the UTF-8 encoding when you save it. When the Python interpreter reads the source code, in order for it to read in UTF-8 encoding, we usually write the two lines at the beginning of the file:

#!/usr/bin/env python
#-*-Coding:utf-8-*-

The first line of comments is to tell the Linux/os x system that this is a Python executable program that the Windows system ignores.

The second line of comments is to tell the Python interpreter to read the source code according to the UTF-8 code, otherwise the Chinese output you write in the source code may be garbled.

Related Article

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.