Python Coding issues

Source: Internet
Author: User

"Hello, world!" is not a problem with Python output, but if you output Chinese characters "Hello, World" you may encounter Chinese coding problem.

If no encoding is specified in the Python file, an error occurs during execution:

#!/usr/bin/python
"Hello, the World";

Run results

The default encoding format in Python is the ASCII format, which fails to print correctly when the encoding format is not modified, so the error occurs when reading Chinese.

The workaround is to add #-*-coding:utf-8-*- or #coding =utf-8 at the beginning of the file.

#!/usr/bin/python
#-*-Coding:utf-8-*-

"Hello, the World";

Run the result as

So if you are in the process of learning that the code contains Chinese, you need to specify the encoding in the header.

Note: python3.x source files use utf-8 encoding by default, so you can parse Chinese normally without specifying UTF-8 encoding.

Note: If you use the editor and you need to set the PY file storage format to UTF-8, you will receive an error message similar to the following:

SyntaxError:(Unicode error)'UTF-8' codec can'byte0xc4 In 0:byte                

Pycharm Setup Steps:

    • Go to file > Settingsand search for encodingin the input box.
    • Locate Editor > File encodings, and set the IDE Encoding and Project Encoding to Utf-8.

Python Coding issues

Related Article

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.