Identifiers in Python: Naming Rules & Best Practices

Python is an object-oriented high-level programming language that has been widely accepted for general-purpose programming. Built by the Python Software Foundation in 1991, it has found its way into industrial-level programming and is currently the #3 most used programming language globally. As per Statista, nearly 48.24% of developers rely on Python to achieve desirable results in data science, making the language one of the most highly desirable data science skills for job seekers, apart from R and SQL. 

The internet is flooded with courses that teach Python within months. Due to the rules mentioned above, the rapid growth of internet learning is flooded with courses on Python, which are now available to a broader audience across all age groups. 

Like all programming languages, Python too has its attributes and rules that need to be followed when creating software applications. This article will discuss identifiers in Python and explore the rules and best practices for naming identifiers. 

What are Identifiers?

Different programming elements need to be identified and named uniquely to differentiate them from others of the same kind. These are referred to as Identifiers. The user defines these names according to their choice and requirements. These include names for classes, functions, variables, methods, and so on.  

For Python, identifiers work similarly, except for a few naming conventions that are unique to the language. 

For example, Python doesn’t approve special characters such as @,%,$, etc., and punctuation marks within identifier names.

Also, identifier names cannot be the same as keywords.

What are Keywords? How are they different from Identifiers?

Reserved words in Python which cannot be used as an identifier like function names or variable names are known as keywords. They are helpful in the definition of the structure and syntax of Python. As of Python 3.7, there are 33 keywords. This number may increase or decrease over time. Except ‘True’, ‘False’, and ‘None’, all other keywords are found in lowercase and need to be used accordingly, keeping the case sensitivity in mind. 

Explore our Popular Data Science Online Courses

Naming rules for Identifiers in Python: How are Identifiers named?

For naming identifiers, Python has a unique set of rules that need to be followed. Here’s taking a look:

  • Identifier names in Python can contain numbers (0-9), uppercase letters (A-Z), lowercase letters (a-z), and underscore (_).  
  • The name should always start with a non-numeric character.
  • An identifier name should not contain numeric characters only.
  • Identifier names in Python are case-sensitive like most other languages. (‘Ash’ is different from ‘ASH’).
  • Users can begin identifiers with an underscore; it will not display an error.
  • An identifier name can be of any length, although the PEP-8 standard rule advises limiting the number of characters in a line to 79.
  • Identifiers should not carry the same name as keywords. (To avoid errors in this regard, users can type help() followed by “keywords” to view a list of all the keywords in Python.)
  • Leading double underscores (__$) cannot be used as these category names are used for the context of the definition of a class. These are private variables of derived and base classes. 

Deviation from any of these rules mentioned above may lead to error reports and cause runtime issues in the program:

Valid Python Identifiers (Examples)

Any combination of numbers, letters, and underscores which comply with the mentioned rules are accepted as valid Python identifiers. Some examples are:

  • _pratt: identifier names can start with an underscore.
  • _: this may look odd, but a single underscore can be used as an identifier name. 
  • alpha123: identifier names can begin with alphabets in lowercase. 
  • DoGs: identifier names can contain uppercase and lowercase alphabets in any pattern.
  • DRE, dre, and Dre will be considered three different identifier names due to the difference in the case.

Invalid Python Identifiers (Examples)

Many identifier names that may be valid in other programming languages are invalid in the case of Python. Some examples are:

  • 999: identify names that cannot contain only digits or numeric values.
  • x+iy: identifier names cannot contain any other special character except underscore.
  • While: identifier names cannot be reserved keywords that have separate meanings for the language.
  • 123alpha: identifier names cannot begin with numbers.

Top Data Science Skills to Learn to upskill

How to check the validity of Identifier names? 

Python has a function which developers can use to check if an identifier name will be declared valid or not. It is the function identifier().

However, the limitation of this function is that it does not consider reserved keywords for identification.

To overcome this limitation, Python provides another function known as keyword identifier(). This function checks the validity of an identifier name while keeping the keywords in mind.

For example; 









True (incorrect output)

There is another function str.isidentifier(), that can determine whether an identifier name is valid or not. 

What are the best practices for Naming Identifiers in Python?

Although following Python’s rules is enough for generating unique identifier names which will be declared valid, professionally, users are suggested to follow a certain naming practice. This reduces minute, unforeseen problems and errors while experimenting with different types of identifier names. Even though these errors may seem negligible and might not report syntax errors initially, they can lead to runtime, or logical errors can occur, consequently displaying unwanted results. 

(For perspective, errors that occur in the code are known as syntax errors. When the syntax is correct, but the logic is wrong – ultimately leading the program towards a different path – the error is known as a runtime error.)

Here are the best naming practices for Identifiers in Python:

1. For Naming Constants:

  • Use all uppercase or capital letters for names.
  • Users can separate words by an underscore. 

2. For Package Names:

  • Short names are preferred.
  • Use of underscores is not advised.
  • All characters should be in lowercase.
  • Example: utilities, math, etc.

Read our popular Data Science Articles

3. For Class Names 

  • It is advised to start class names with uppercase letters. For example, Matrix, Transpose, Shuffle, etc. 
  • For class names having multiple words, users can use capital letters for the starting alphabet of each word. For example, BubbleSort, ElectricBill, StandardData. 

Apart from this Following are the Best Naming Practices for Identifiers in Python

  • If the identifier consists of two underscores, one at the beginning and one at the end, the identifier name is a language-defined special. Users should avoid this technique of naming.
  • Generally, names of functions that return Boolean values begin with ‘is’. For example, isstring, iskeyword, etc.
  • Identifier names can be of any length. But one should keep it short and precise for efficient usage. Like, First_inputed_value is acceptable, but it is better to use InpValue_1
  • Identifier names should be kept meaningful for a better understanding of the program. To provide examples, ‘HRAvalue: conveys the underlying message better than ‘Asdf0022’.
  • Technically, one can use underscores as the first and last characters, but it is advised not to do so because that format is used for Python built-in types.
  • If the names of variables models for functions contain more than one word, then it is better to separate them with an underscore. Example: is_true(), Input_array(), object_inputted, etc. 
  • Generally, module functions and variable names begin with lowercase alphabets. For example: dataentry(), pattern_1, etc. 

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights


Python is one of the most widely used programming languages of the current decade. The ease of use it facilitates while reducing complexities in coding has provided it with an edge over other contemporary programming languages. 

Naming identifiers is one of the primary elements to understand when learning to code in Python. Identifiers, being user-defined names, are unique for every program. As long as the rules of naming are followed, the names are good to go. However, to increase efficiency within a given time frame, it is advised to follow certain naming practices which are universally accepted. We hope this article helped you learn about the basics of Python identifiers.

If you are interested in gaining more knowledge on Python’s building blocks, we recommend joining upGrad’s Data Science Program from IIIT Bangalore. The 18-month course comprises 500+ hours of content, 60+ case studies and projects, and global access to fantastic job opportunities. After completing this course, you can take on roles like ​​Data Analyst, Data Scientist, Product Analyst, Machine Learning Engineer, Business Analyst. 

Get in touch with us today to enrol in the program!

What are valid identifiers in Python?

Identifiers in Python can comprise both lowercase (a to z) and uppercase letters (A to Z). They can also include digits (0 to 9) and start with an underscore (_).

Give examples of valid identifiers in Python.

Here are a few examples of valid identifiers in Python:
2. _789
3. userName
4. enter_user_details

Can keywords be used as identifiers?

No, identifiers can not have the same names as keywords. Note that there are 33 keywords in Python.

Want to share this article?

Be A Master Of Data Science with University of Arizona

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks