Reference URL: https://www.xncoding.com/2015/10/24/python/unicode.html
In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.
When editing with Notepad, the UTF-8 characters read from the file are converted to Unicode characters into memory, and when the edits are complete, the conversion of Unicode to UTF-8 is saved to the file:
When you browse the Web, the server converts dynamically generated Unicode content to UTF-8 and then to the browser:
So you see a lot of pages of the source code will have similar information, that the page is exactly the UTF-8 encoding
>>> Print (' str with Chinese ')
Chinese-included str
>>> ' ABC '. Encode ( b ' ABC '
>> > "Chinese". Encode ( ' utf-8 ')
>>> ' 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 (+)
B ' ABC '. Decode (' ASCII ')
' ABC '
B ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' utf-8 ')
Chinese
When manipulating strings, we often encounter mutual conversions between Str and bytes. To avoid garbled problems, you should always use UTF-8 encoding to convert str and bytes.
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 basics of Python learning: coding