First, the previous picture:
We know: 1 bytes = 8 bits
Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and ordinary strings were ‘ABC‘
ASCII-encoded inside python. Python provides the Ord () and Chr () functions to convert letters and corresponding numbers to each other:
>>> ord(‘A‘)65>>> chr(65)‘A‘
Python later added support for Unicode, expressed in Unicode as a string u‘...‘
, for example:
>>> print u‘中文‘中文>>> u‘中‘u‘\u4e2d‘
Writeu‘中‘
Andu‘\u4e2d‘
is the same,\u
The hexadecimal Unicode code is followed. Sou‘A‘
Andu‘\u0041‘
is the same.
How do two strings convert to each other? Although the string ‘xxx‘
is ASCII encoded, it can also be seen as UTF-8 encoding, and u‘xxx‘
only Unicode encoding.
u ' xxx '
converted to UTF-8 encoded ' xxx '
with encode (' Utf-8 ')
method:
>>> u ' abc '. Encode (' utf-8 ') ' abc ' >>> u ' Chinese '. Encode (' utf-8 ') ' \xe4\ xb8\xad\xe6\x96\x87 '
English characters are converted to represent UTF-8 values that are equal to Unicode values (but occupy different storage spaces), while Chinese characters convert 1 Unicode characters into 3 UTF-8 characters, and you see \xe4
is one of the bytes, because its value is 228
, no corresponding letter can be displayed, so the numeric value of the byte is displayed in hexadecimal. len ()
function can return the length of a string:
>>> len (U ' abc ') 3>>> len (' abc ') 3>>> len (U ' Chinese ') 2>> > Len (' \xe4\xb8\xad\xe6\x96\x87 ') 6
in turn, the UTF-8 code represents the string ' xxx '
converted to a Unicode string u ' xxx '
with 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‘)中文
Because the Python source code is also a text file, so when your source code contains Chinese, it is important to specify that you save it as UTF-8 encoding when you save it. When the Python interpreter reads the source code, in order for it to be read by UTF-8 encoding, we usually write these 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 and the Windows system ignores this comment;
The second line of comments is to tell the Python interpreter to read the source code according to the UTF-8 encoding, otherwise the Chinese output you write in the source code may be garbled.
Python third-party Library series 15--code library