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.