Home > Backend Development > Python Tutorial > How to add Chinese comments when programming in python

How to add Chinese comments when programming in python

(*-*)浩
Release: 2019-07-05 11:07:47
Original
5000 people have browsed it

How to add Chinese comments when programming in python

Recently developed and learned Pyton. When adding Chinese comments, the running program reported an error:

File "red.py", line 10
SyntaxError: Non-ASCII character '\xe5' in file red.py on line 10, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Copy after login

Solution:
In the Python script file Add a sentence to the first or second line:

# -*- coding: utf-8 -*-
Copy after login

Since the Python source code is also a text file, when your source code contains Chinese, when saving the source code, Be sure to specify saving as UTF-8 encoding. When the Python interpreter reads the source code, in order for it to be read in UTF-8 encoding, we usually write these two lines at the beginning of the file:

#!/usr/bin/env python   //为了告诉Linux系统,这个是python可执行程序
# _*_ coding:utf-8 _*_  //为了告诉python解释器,按照utf-8编码读取源代码,否则,你在源代码中写的中文输出可能会由乱码
#coding=utf-8 //上一行代码也可以写成这种形式
Copy after login

For more Python-related technical articles, please visit PythonTutorial column to learn!

The above is the detailed content of How to add Chinese comments when programming in python. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template