What are the rules for naming valid Python identifiers?

Learn the rules and conventions like snake_case and PascalCase for naming valid identifiers like variables and functions in Python
On this page

What are the rules for naming valid Python identifiers?

Excerpt

Identifiers like variable and function names in Python must follow certain rules - they can only contain letters, digits and underscores but cannot start with a digit. Conventions like snake_case improve code readability.


In Python, identifiers are used to name variables, functions, classes, modules and other objects. Identifiers make code more readable by allowing developers to choose intuitive, meaningful names. However, there are some rules and conventions that must be followed when naming identifiers in Python. Understanding these rules will help ensure you write valid, properly structured Python code.

What is an Identifier?

An identifier is a name given to entities like variables, functions, classes, etc. in Python. It can be composed of letters, digits and underscores. Identifiers are used to label these objects in a program so you can refer to them later. For example:

1my_variable = 5
2def my_function():
3  print("Hello")
4class MyClass:
5  x = 10

Here my_variable, my_function and MyClass are identifiers.

Naming Rules for Python Identifiers

There are some important rules to follow when naming identifiers in Python:

  • Can contain alphanumeric letters (a-z, A-Z) and underscores (_)
  • Cannot start with a digit (0-9)
  • Case sensitive (myvar and MyVar are different identifiers)
  • Cannot be a Python keyword (for, while, class etc)

Additionally, here are some free online tools that can help with case conversion:

Using these tools can help ensure you follow naming conventions properly.

Valid Identifier Examples

Here are some examples of valid identifiers in Python:

1my_variable = "IToolkit"
2accountNumber = 12345
3MAX_SIZE = 100
4_private_variable = 0

These follow all the rules mentioned above.

Invalid Identifier Examples

And here are some invalid identifiers:

11var = 10 # cannot start with digit
2my-variable = 5 # no hyphens allowed
3for = 10 # 'for' is a Python keyword
4Var = 5 # different from var due to case sensitivity

Trying to use any of these will result in a SyntaxError in Python.

Naming Conventions and Styles

Beyond just following the rules, it’s important to use proper naming conventions and styles for readability. Here are some common conventions:

Using these conventions consistently will help make your code easier to understand. The online case conversion tools mentioned earlier can help convert strings to the proper naming convention.

Summary

To summarize Python’s identifier naming rules:

  • Can only contain alphanumeric letters and underscores
  • Cannot start with a number
  • Are case sensitive (myvar and MyVar are different)
  • Cannot use Python keywords as identifier names

It’s important to follow these rules to avoid errors. Additionally using naming conventions like snake_case and PascalCase improves code readability.

Clear, intuitive identifier names make Python code more maintainable and robust. Following these naming rules and conventions is essential to writing valid, professional Python code. The online case conversion tools can help ensure you properly follow Python’s naming conventions.