The string also has an encoding problem.
Because a computer can only handle numbers, if you are working with text, you must convert the text to a number before processing it. The oldest computer was designed with 8 bits (bit) as a byte (byte), so the largest integer that a Word energy saver represents is 255 (binary 11111111 = decimal 255), and 0-255 is used to denote uppercase and lowercase letters, numbers, and some symbols. This Code table is called ASCII encoding, such as the code for capital A is 65, and the lowercase z is encoded as 122.
If you want to express Chinese, obviously a byte is not enough, need at least two bytes, and also can't and ASCII encoding conflict, so, China has developed GB2312 code, used to put Chinese into.
Similarly, other languages such as Japanese and Korean also have this problem. In order to unify all text encoding, Unicode came into being. Unicode unifies all languages into a set of encodings, so there is no more garbled problem.
Unicode usually uses two bytes to represent a character, the original English encoding from a single byte into a double-byte, only need to fill the high byte all 0 can be.
Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and the normal string ' ABC ' was ASCII-encoded inside python.
Python later added support for Unicode, with a Unicode-represented string expressed in U ' ... ', for example:
Print U ' Chinese '
Note: Without u, Chinese will not display properly.
In addition to a single Unicode string u
, the escape character and multiline notation are still valid, with no difference to the normal string:
Escape:
U ' chinese \ n \ japanese \ Korean '
MultiRow
U ' ' first line second line '
raw+ Multiple lines:
ur ' python's Unicode string supports "Chinese", "Japanese", "Korean" and many other languages "
If the Chinese string encounters unicodedecodeerror in a Python environment, this is because there is a problem with the format saved by the. py file. You can add comments on the first line
#-*-Coding:utf-8-*-
The purpose is to tell the Python interpreter to read the source code with UTF-8 encoding. Then save with notepad++ as ... and select UTF-8 format to save.
Unicode strings in Python