Python Identifiers (Rules and Examples)


5 min read

An identifier is a user-defined name given to a variable, function, class, module, or any other object in a Python program. It is a sequence of characters, which can include letters, digits, and underscores (_), and must follow certain rules.

Identifiers Rules List

Here is a list of 6 rules to name variables in python.

Rule 1The first character of an identifier must be a letter or an underscore.
Rule 2The rest of the identifier can be composed of letters, digits, and underscores.
Rule 3Identifiers are case-sensitive.
Rule 4Python keywords cannot be used as identifiers.
Rule 5Identifiers should be descriptive and meaningful.
Rule 6Identifiers should not be too long or too short.

Let's cover each rule in detail

๐Ÿ’ก Complete Python Roadmap for beginners and experts.

Rule 1

The first character of an identifier must always be a letter (uppercase or lowercase) or an underscore (_). It cannot be a digit or any other special character. For example:

myVar   # valid identifier
_myVar  # valid identifier
2ndVar  # invalid identifier

In the above example, myVar and _myVar are valid identifiers because they start with a letter and an underscore, respectively. However, 2ndVar is an invalid identifier because it starts with a digit.

Rule 2

After the first character, the rest of the identifier can be composed of letters (uppercase or lowercase), digits (0-9), and underscores (_). It cannot contain any other special character or whitespace. For example:

num2        # valid identifier
my_var      # valid identifier
my-Var      # invalid identifier
my Var      # invalid identifier

In the above example, num2 and my_var are valid identifiers because they are composed of letters, digits, and underscores. However,** my-Var** and my Var are invalid identifiers because they contain a hyphen and a whitespace, respectively.

Rule 3

Identifiers in Python are case-sensitive, which means that myVar and MyVar are two different identifiers. For example:

myVar   # valid identifier
MyVar   # valid identifier (different from myVar)

In the above example, myVar and MyVar are both valid identifiers because they follow the rules for identifiers in Python. However, they are two different identifiers because they differ in their case.

Rule 4

Python keywords are reserved words that have a specific meaning in the Python language. They cannot be used as identifiers, which means that they cannot be used to name variables, functions, classes, or any other object in a Python program. For example:

if      # invalid identifier (Python keyword)
for     # invalid identifier (Python keyword)
while   # invalid identifier (Python keyword)

In the above example, if, for, and while are Python keywords and cannot be used as identifiers in a Python program.

Rule 5

Identifiers should be descriptive and meaningful so that the code can be easily understood by others. They should reflect the purpose of the object they identify. For example:

person_age      # meaningful identifier
num1            # not very descriptive

In the above example, person_age is a meaningful identifier because it describes the purpose of the variable it identifies. However, num1 is not very descriptive and does not provide any information about the purpose of the variable.

Rule 6

Identifiers should be neither too long nor too short. It is recommended to keep them between 2 and 15 characters long. For example:

list_of_names       # good identifier
this_is_a_really_long_identifier   # too long
a              # too short

In the above example, list_of_names is a good identifier because it is descriptive and not too long or too short. However, this_is_a_really_long_identifier is too long and a is too short.

๐Ÿ’ก [Optional] You can refer to these guides to install python and virtual environment.

Syntax for identifiers in Python

identifier ::=  (letter|"_") (letter | digit | "_")*

In this syntax, identifier is the name given to the identifier, letter represents any uppercase or lowercase letter, and digit represents any digit from 0 to 9. The underscore character (_) is also allowed in an identifier, but it cannot be used as the first character of an identifier.

Identifiers Best Practices

You can write more readable variable names if you follow the best practices.

  • Use descriptive and meaningful names.

  • Follow naming conventions.

  • Avoid using single-character names.

  • Keep names short and clear.

  • Use correct spelling and grammar.

๐Ÿ’ก Tip: Use CamelCase for class names, lowercase with underscores for variable and function names, and uppercase with underscores for constants.

Difference between Identifiers, Variables and Keywords in Python

DefinitionNames that identify objects in Python.Used to store values and data.Reserved words in Python with a specific meaning.
ExamplesmyVar, num1, list_of_namesx = "Nitin", y = True, z = 0if, else, for, while, class, def
UsageName objects in Python.Store values and data in a program.Define the syntax and structure of a Python program.
Data typeDo not have a data type.Have a data type.N/A
ScopeN/AHave a scope that determines their access.N/A

In summary, an identifier in Python must follow the above rules and can be used to name variables, functions, classes, and other objects in your Python code.

Did you find this article valuable?

Support Nitin Raturi by becoming a sponsor. Any amount is appreciated!