Valid variable names and naming rules in Python

Modified: | Tags: Python

In Python, identifiers (= variable names, function names, class names, etc.) need to be defined according to rules. Names that do not follow the rules cannot be used as identifiers.

The following is for Python 3 and may be different for Python 2.

Valid characters for identifiers (= names)

Here, the characters that can and cannot be used for identifiers (= variable names, function names, class names, etc.) are shown.

Basically, just remember the following.

  1. Uppercase and lowercase alphabets, numbers, and the underscore (_) can be used.
  2. Numbers cannot be used as the first character.

Although non-alphabetic characters like Kanji can be used, it's recommended to avoid them unless there is a compelling reason.

ASCII

ASCII characters that can be used for identifiers are uppercase and lowercase alphabets (A-Z, a-z), numbers (0-9), and the underscore (_).

AbcDef_123 = 100
print(AbcDef_123)
# 100

Symbols other than the underscore cannot be used.

# AbcDef-123 = 100
# SyntaxError: can't assign to operator

Numbers cannot be used as the first letter.

# 1_abc = 100
# SyntaxError: invalid token

The underscore can be used for the first letter.

_abc = 100
print(_abc)
# 100

Note that the underscore at the beginning may have a special meaning.

Unicode

In Python 3, Unicode characters such as Kanji and Hiragana can be used.

変数その1 = 100
print(変数その1)
# 100

Not all Unicode characters can be used. For example, you cannot use emoji.

# ☺ = 100
# SyntaxError: invalid character in identifier

See the official documentation for the Unicode category codes that can be used.

Check if the string is a valid identifier: isidentifier()

You can check whether a string is a valid identifier with the isidentifier() method of the string (str).

It returns True if the string is a valid identifier and False if not.

print('AbcDef_123'.isidentifier())
# True

print('AbcDef-123'.isidentifier())
# False

print('変数その1'.isidentifier())
# True

print('☺'.isidentifier())
# False

Words that cannot be used as identifiers: Reserved words and keywords

Reserved words and keywords are valid as identifiers but cannot be used as ordinary identifiers.

Note that isidentifier() returns True for reserved words and keywords since they are valid strings. However, using them as identifiers (variable names, function names, class names, etc.) will raise an error.

print('None'.isidentifier())
# True

# None = 100
# SyntaxError: can't assign to keyword

To get a list of keywords and check if a string is a keyword, use the keyword module of the standard library. See the following article.

Words that should not be used as identifiers

The names of built-in functions can be used as identifiers, so you can assign new values to them.

For example, len() is a built-in function that returns the number of elements in a list or the number of characters in a string.

print(len)
# <built-in function len>

print(len('abc'))
# 3

If you assign a new value to the name len, the original function is overwritten. Note that no error or warning is printed when assigning.

print(len('abc'))
# 3

len = 100
print(len)
# 100

# print(len('abc'))
# TypeError: 'int' object is not callable

A common mistake is writing list = [0, 1, 2], which prevents the use of list().

For more information on checking the list of built-in functions and constants, see the following article.

Naming conventions (PEP8)

PEP stands for Python Enhancement Proposal.

PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment.
PEP 1 – PEP Purpose and Guidelines | peps.python.org

PEP8 describes the "Style Guide for Python Code".

Naming conventions are also mentioned.

For example, the following styles are recommended. See the link above for details.

  • Module names
    • lowercase_underscore
  • Package names
    • lowercase
  • Class and exception names
    • CapitalizedWords (CamelCase)
  • Function and variable and method Names
    • lowercase_underscore
  • Constants
    • ALL_CAPS

If your organization does not have its own naming conventions, it is recommended to follow PEP8.

Related Categories

Related Articles