What are Identifiers in Python?

In this tutorial, we will answer the question - what are Identifiers in Python. An identifier is a user-defined name given to identities like class, functions, variables, modules, or any other object in Python. Here we will discuss the rules for writing identifiers.

Table of contents

Rules for writing Identifiers in Python

  1. The identifier is a combination of character digits and underscore and the character includes letters in lowercase (a-z), letters in uppercase (A-Z), digits (0-9), and an underscore (_).
  2. An identifier cannot begin with a digit. If an identifier starts with a digit, it will give a Syntax error.
  3. In Python, keywords are the reserved names that are built-in to Python, so a keyword cannot be used as an identifier - they have a special meaning and we cannot use them as identifier names.
  4. Special symbols like !, @, #, $, %, etc. are not allowed in identifiers.
  5. Python identifiers cannot only contain digits.
  6. There is no restriction on the length of identifiers.
  7. Identifier names are case-sensitive.

Python Valid Identifiers Example

  • abc123
  • abc_de
  • _abc
  • ABC
  • abc

Python Invalid Identifiers Example

  • 123abc
  • abc@
  • 123
  • for

Python Keywords

Here is the list of some reserved keywords in Python that cannot be used as identifiers.


As you can see here all the keywords except 'True', 'False', and 'None' are in lowercase, therefore they must be written as they are.

Testing the Validity of Python Identifiers

The str.isidentifier() function is used to check the validity of an identifier but, this method doesn’t take reserved keywords into consideration. So, we can use this function with keyword.iskeyword() to check if the name is valid or not.


print ("abc".isidentifier())
print ("123abc".isidentifier())
print ("_abc".isidentifier())
print ("for".isidentifier())



Now, we know “for” is a reserved keyword so, it is an invalid identifier.


def is_valid_identifier(x):
    Return x.isidentifier() and not keyword.iskeyword(x)



Python Identifier Naming Best Practices

  1. Only class names are started with capital letters (Student, Employee).
  2. Multiple words in a variable are separated by an underscore (is_valid()).
  3. If an identifier begins with an underscore, it means it is a private identifier. This does not make the variable private. It is only for the ease of the programmer to easily distinguish between private variables and public variables.
  4. Python built-in magic methods use double underscores around the names (__len__). Therefore, double underscores are used only when you are dealing with mangling in Python.
  5. Using longer name than one character is always preferred (index = 1 is better than i = 1).
  6. Camel case are used while naming the variables (studentAddress, studentId). Camel case use a capital letter to begin the second word in a compound name or phrase, when it is not separated from the first word by a space.

Closing Thoughts on Python identifiers

Python Identifiers are user-defined names. We discussed the rules and some best practices while naming identifiers that are good for fellow Python programmers. One can learn about more Python concepts here.

// Find jobs by category

You've got the vision, we help you create the best squad. Pick from our highly skilled lineup of the best independent engineers in the world.

Copyright @2023 Flexiple Inc