View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Identifiers in Python: Naming Rules & Best Practices

By Pavan Vadapalli

Updated on Mar 07, 2025 | 7 min read | 15.0k views

Share:

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, including names for classes, functions, variables, methods, and more.

In Python, identifiers follow similar principles but also adhere to specific naming conventions unique to the language. Understanding what are identifiers in Python is essential for writing clean and efficient code. Python identifiers must begin with a letter (A-Z or a-z) or an underscore (_) and can be followed by letters, digits (0-9), or underscores. However, they cannot be Python keywords or contain special characters like @, #, or $.

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. 

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:

Check out all trending Python tutorial concepts in 2024

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.

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; 

print(“xyz”.isidentifier()) 

print(“88x”.isidentifier()) 

print(“_”.isidentifier())

print(“while”.isidentifier())

Output:

True

False

True

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. 
  • Example: MAX_VALUE, SUMMATION_INDEX, etc.

2. For Package Names:

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

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree18 Months
View Program

Placement Assistance

Certification8-8.5 Months
View Program

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

Also, Read Identity Operator in Python

Conclusion

Python is one of the most widely used programming languages of the current decade. Its ease of use and ability to simplify complex coding tasks give it an edge over other contemporary languages.

Understanding identifiers in Python is essential for writing efficient and error-free code. Since identifiers are user-defined names, they must be unique within a program. As long as the naming rules are followed, these identifiers can be used without issues. However, to enhance readability and maintainability, it is advisable to follow universally accepted naming conventions.

We hope this article helped you understand what are identifiers in Python and how to use them effectively. Happy coding!

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!

Frequently Asked Questions (FAQs)

1. What is the identifier type?

2. What is a unique identifier in Python?

3. What is a keyword in Python?

4. Are Python identifiers case-sensitive?

5. Can Python identifiers start with a number?

6. How do underscores (_) work in Python identifiers?

7. Can Python identifiers contain special characters?

8. Is there a length limit for Python identifiers?

9. Can I use spaces in Python identifiers?

10. How do I check if a name is a keyword in Python?

11. What happens if I use a reserved word as an identifier?

Pavan Vadapalli

899 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in Data Science & AI

Placement Assistance

Executive PG Program

12 Months

View Program
Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Dual Credentials

Master's Degree

18 Months

View Program
upGrad Logo

Certification

3 Months

View Program