Home >Backend Development >Python Tutorial >python why encoding statement
The reason why python needs to encode the statement: 1. The first line in python is to indicate what executable program the code in this file uses to run it; 2. If you want to write it in the py file of python2 Chinese, you must add a line of comments declaring the file encoding, otherwise python2 will use ASCII encoding by default.
【Related learning recommendations: python tutorial】
The reason why python needs to encode the statement:
The first line in python is to point out what executable program you want the code in your file to use to run it, just So simple
#!/usr/bin/python
is equivalent to hard-coding the python path (it tells the OS to call the python interpreter under /usr/bin when executing this py;)
#!/usr/bin/env python
will go to the environment settings to find the python directory. This writing method is recommended (the purpose is to prevent the user from not installing python in the default /usr/ for the OS) bin path. Meaningful in a Linux-like OS environment)
If you want to write Chinese in the py file of python2, you must add a line of comments declaring the file encoding, otherwise python2 will use ASCII encoding by default.
# -*- coding:utf-
It should be noted that in addition to the parsing path should be placed on the first line of py, #coding:utf-8, which is generally used to specify the file encoding in Python, should also be specified when needed. , you can put #coding:utf-8 on the second line:
#!/usr/bin/env python # -*- coding:utf-
If you want to know more about related learning, please pay attention to the php training column!
The above is the detailed content of python why encoding statement. For more information, please follow other related articles on the PHP Chinese website!