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
- Valid Identifiers Example
- Invalid Identifiers Example
- Python Keywords
- Testing the Validity of Python Identifiers
- Python Identifier Naming Best Practices
- Closing Thoughts
Rules for writing Identifiers in Python
- 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 (_).
- An identifier cannot begin with a digit. If an identifier starts with a digit, it will give a Syntax error.
- 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.
- Special symbols like !, @, #, $, %, etc. are not allowed in identifiers.
- Python identifiers cannot only contain digits.
- There is no restriction on the length of identifiers.
- 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())
Output:
TRUE
FALSE
TRUE
TRUE
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"))
Output:
FALSE
Python Identifier Naming Best Practices
- Only class names are started with capital letters (Student, Employee).
- Multiple words in a variable are separated by an underscore (is_valid()).
- 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.
- 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.
- Using longer name than one character is always preferred (index = 1 is better than i = 1).
- 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.