python why encoding statement

coldplay.xixi
Release: 2020-08-29 11:34:20
Original
3035 people have browsed it

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.

python why encoding statement

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/pythonis 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 pythonwill 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-
Copy after login

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-
Copy after login

If you want to know more about related learning, please pay attention to thephp trainingcolumn!

The above is the detailed content of python why encoding statement. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!