Home >Backend Development >Python Tutorial >What are the rules for legal variable names in python?

What are the rules for legal variable names in python?

王林
王林Original
2020-07-03 09:33:0732020browse

The rules for legal variable names in python are: 1. It can be composed of letters, numbers, and underscores. At the same time, it cannot start with a number; 2. It cannot be a Python keyword, but it can contain keywords; 3. It cannot Contains spaces. For example: [a1c_x2z].

What are the rules for legal variable names in python?

Python needs to use identifiers to name variables. In fact, identifiers are symbols used to name variables, classes, and methods in the program (identifiers are legal name).

(Recommended learning: python tutorial)

Python identifier rules are as follows:

  • Identifiers can consist of letters and numbers , composed of underscores (_), in which numbers cannot begin.

  • Identifiers cannot be Python keywords, but can contain keywords.

  • Identifiers cannot contain spaces.

Because Python 3 supports the UTF-8 character set, Python 3 identifiers can use characters in multiple languages ​​that UTF-8 can represent. The Python language is case-sensitive, so abc and Abc are two different identifiers.

Let’s look at some examples:

  • abc_xyz: Legal.

  • HelloWorld: Legal.

  • abc: Legal.

  • xyz#abc: Illegal, "#" is not allowed in the identifier.

  • abc1: Legal.

  • 1abc: Illegal, the identifier does not allow numbers to begin with.

The above is the detailed content of What are the rules for legal variable names in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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