DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

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.

False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not await
class form or async
continue global pass

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.

Input:

print ("abc".isidentifier())
print ("123abc".isidentifier())
print ("_abc".isidentifier())
print ("for".isidentifier())
Enter fullscreen mode Exit fullscreen mode

Output:

TRUE
FALSE
TRUE
TRUE
Enter fullscreen mode Exit fullscreen mode

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

Input:

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

print(is_valid_identifier("for"))

Enter fullscreen mode Exit fullscreen mode




Output:

FALSE
Enter fullscreen mode Exit fullscreen mode




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.

Top comments (1)

Collapse
 
samueljevix profile image
samueljevix

hello , am lugya samuel jevix from Uganda am a self teaching sofware developer strugling to learn python kivy,kivymd and others am kindly requesting your help please , that you give me your hand and save me from strugling and become a good software developer: thanks hope a positive response