For just beginning to touch
pythonIn this language, there may be some confusion about how to use this language for programming. How to use the Chinese language to write the program, will not be the same as the writing of the English place? Today we'll look at how to use Chinese in Python to express what you're thinking.
For most programming languages, the first starter code is "Hello world! "The following code is output using Python" Hello world! ":
#!/usr/bin/pythonprint "Hello, world!";
After entering the above code, you will see the following result:
Hello, world!.
And if the input is not English but Chinese "Hello, the world!" , and the input will be as follows:
#!/usr/bin/pythonprint "Hello, world!";
The result of the output is:
File "test.py", line 2syntaxerror:non-ascii character ' \xe4 ' in file test.py on line 2, but no encodingdeclared; See http://www.python.org/peps/pep-0263.html for details
Warning error, unable to output Chinese.
The reason for this result is that the default encoding format in Python is ASCII, and the Chinese characters are not printed correctly when the encoding format is not modified, so the error occurs when reading Chinese.
The solution is simply to add the #-*-Coding:utf-8-*-or #coding=utf-8 to the beginning of the file to solve the problem.
(Note: #coding =utf-8 = no spaces on either side.) )
After adding the above code at the beginning of the file, try outputting "Hello, world!" ”
#!/usr/bin/python#-*-coding:utf-8-*-print "Hello, world";
In this case, the output will turn out to look as follows:
Hello, world!
This is the way the Chinese code of Python, today's content is this, hoping to help you learn.