Look Liao Xuefeng teacher python blog notes - string and coding

1. In computer memory, uniform use Unicode encoding, or when it is desired to be transmitted to the hard disk when it is converted to UTF-8 encoding;
browse the Web, the server will dynamically generated content into Unicode UTF-8 again streamed to the browser.
2. The latest version of Python 3, a string encoded in Unicode, that is, Python strings supports multi-language; ord () function to get the character of the integer, chr () function to convert the corresponding character encoding .
3. (1) to distinguish between 'ABC' and b'ABC '(python indicates the type of data bytes), the former is str, although the latter and the former contents have the same display, but only the bytes for each character occupies a byte. If we read the byte stream from the network or disk, then the data is read bytes. Bytes should become str, you require a decode () method:
>>> b'ABC'.decode ( 'ASCII')
as opposed to encode () method

(2) bytes if only a small part of an invalid byte, may be passed errors = 'ignore' ignore errors bytes:

>>> b'\xe4\xb8\xad\xff'.decode('utf-8', errors='ignore')

'In'
4. len () function calculates the number of characters is str
5. Format:
(. 1) >>> 'the Hi,% S,% $ D have have you.'% ( 'Michael', 1000000)
'the Hi , Michael, you have have $ 1000000 '.
(2) the format ():
>>>' the Hello, {0}, performance improves {1: .1f} '. format % (' Bob ', 17.125)
' the Hello, Xiaoming, results improved% 17.1 '
>>> "the Hello, {} and {}!." the format ( "John", "Mary")
>>> "the Hello, {} and {Boy Girl}!." the format (Boy = "John", Girl = "Mary")
>>> "My CAR IS 0.color {}.." the format (black_car)
>>> "of The First Student Student {IS [0]}.." the format (Student = stu_list)
>>> "John IS {D [John]} Old years.". the format (D = age_dict)
// so this is a more formatting method as function calls
Published 19 original articles · won praise 2 · Views 5177

Guess you like

Origin blog.csdn.net/gunjiu4462/article/details/79863730