Joke Collection Website - Public benefit messages - What’s the matter with Chinese garbled characters in python?

What’s the matter with Chinese garbled characters in python?

Python reports an error invalid character in identifier, which means "invalid character in identifier". Check whether any characters are Chinese, just change the Chinese characters to English characters and then run.

The author of Python intentionally designed a very restrictive syntax so that bad programming habits (such as not indenting the next line to the right in an if statement) cannot be compiled. One of the most important ones is Python's indentation rules.

One difference from most other languages ??(such as C) is that the boundaries of a module are completely determined by the position of the first character of each line in this line (while C language uses a pair of Curly braces {} are used to clearly define module boundaries and have nothing to do with the position of characters). This has been controversial.

Because since the birth of languages ??like C, the grammatical meaning of the language has been separated from the arrangement of characters, which was once considered an improvement in programming languages. However, it is undeniable that Python does make programs clearer and more beautiful by forcing programmers to indent (including all places where modules need to be used, such as if, for, and function definitions).

Extended information

Python Chinese encoding:

The default encoding format in Python is ASCII format. Chinese characters cannot be printed correctly without modifying the encoding format, so in An error will be reported when reading Chinese.

The solution is to just add ?# -*- coding: UTF-8 -*-?or ?# coding=utf-8? at the beginning of the file

Note: # There should be no spaces on either side of the ?=? sign in coding=utf-8?.

Example:

(Python 2.)

#!/usr/bin/python

# -*- coding: UTF -8 -*-? print( "Hello, world" )

The output result is:

Hello, world