Python Chinese output error Resolution (example tutorial)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.