A Python string
In the latest version of Python 3, strings are encoded in Unicode, meaning that Python strings support multiple languages, such as:
>>> print (' str with Chinese ') contains Chinese str
For the encoding of a single character, Python provides an ord() integer representation of the function to get the character, and the chr() function converts the encoding to the corresponding character:
>>> Ord (' A ') 65>>> ord (' Middle ') 20013>>> chr ("The ' B ' >>> chr (25991) ' text ')
If you know the integer encoding of a character, you can write it in hexadecimal.str
' \u4e2d\u6587 '//Chinese
Byte
Because the Python string type is str , in memory, in Unicode, one character corresponds to a number of bytes. If you want to transfer on a network, or save to disk, you need to turn it str into bytes bytes .
Python bytes uses b a prefixed single or double quotation mark for data of type:
x = B ' ABC '
要注意区分‘ABC‘和b‘ABC‘,前者是str,后者虽然内容显示得和前者一样,但bytes的每个字符都只占用一个字节。
The str pass method, expressed in Unicode encode() , can be encoded as specified bytes , for example:
>>> ' abc '. Encode (' ASCII ') b ' abc ' >>> ' Chinese '. Encode (' utf-8 ') b ' \xe4\xb8\xad\xe6\x96\x87 ' >>> ' Chinese '. Encode (' ASCII ') Traceback (most recent call last): File "<stdin>", line 1, in <module> Unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 0-1: Ordinal not in range (128)
Pure English str can be ASCII encoded as bytes , content is the same, containing Chinese str can be UTF-8 encoded as bytes . strcannot be encoded in Chinese ASCII because the range of Chinese encoding exceeds the range of the ASCII encoding, and Python will make an error.
In bytes , the bytes that cannot be displayed as ASCII characters are \x## displayed.
Conversely, if we read the byte stream from the network or disk, then the data read is bytes . To turn bytes str it into, you need to use the decode() method:
>>> b ' abc '. DECODE (' ASCII ') ' abc ' >>> b ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' utf-8 ') ' Chinese '
To calculate str how many characters are included, you can use a len() function
>>> len (' ABC ') 3>>> len (' Chinese ') 2
len()The function calculates the str number of characters, and if bytes so, the len() function calculates the number of bytes
>>> Len (b ' ABC ') 3>>> len (b ' \xe4\xb8\xad\xe6\x96\x87 ') 6>>> len (' Chinese ' encode (' Utf-8 ')) 6
1 Chinese characters are UTF-8 encoded and typically consume 3 bytes, while 1 English characters take up only 1 bytes.
We often encounter str and convert to and bytes from each other when manipulating strings. In order to avoid garbled problems, we should always adhere to the use of UTF-8 encoding str and bytes conversion.
The Python source code is also a text file, so when you save the source code, you need to be sure to save it as UTF-8 encoding when you include Chinese in your source. 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 python3#-*-coding:utf-8-*-
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.
Formatting:
In Python, the format used is consistent with the C language, and is implemented as an % example:
Format% (.... params)
>>> ' Hello,%s '% ' world ' Hello, world ' >>> ' Hi,%s, you have $%d. '% (' Michael ', 1000000) ' Hi, Michael, y The OU has $1000000. '
%The operator is used to format the string. Inside the string, the representation is replaced by a %s string, %d represented by an integer substitution, %x representing a 16-decimal integer, with several %? placeholders, followed by several variables or values, in order to correspond well. If there is only one %? , the parentheses can be omitted.
formatting integers and floating-point numbers can also specify whether to complement 0 and the number of digits of integers and decimals:
>>> '%2d-%02d '% (3, 1) ' 3-01 ' >>> '%.2f '% 3.1415926 ' 3.14 '
Sometimes, % what about a normal character inside a string? This time you need to escape and use it %% to represent one % :
>>> ' growth rate:%d percent '% 7 ' growth rate:7% '
Python encoding format