What are Identifiers in Python?
Tags: python, programming, tutorial, beginners
Published: 2022-06-15 08:47:39

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](#rules-for-writing-identifiers-in-python)
*   [Valid Identifiers Example](#python-valid-identifiers-example)
*   [Invalid Identifiers Example](#python-invalid-identifiers-example)
*   [Python Keywords](#python-keywords)
*   [Testing the Validity of Python Identifiers](#testing-the-validity-of-python-identifiers)
*   [Python Identifier Naming Best Practices](#python-identifier-naming-best-practices)
*   [Closing Thoughts](#closing-thoughts-on-python-identifiers)

## 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
<span id="section3"></span>
*   abc

## Python Invalid Identifiers Example

*   123abc
*   abc@
*   123
<span id="section4"></span>
*   for

## Python Keywords

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

<table>

<tbody>

<tr>

<td>False</td>

<td>def</td>

<td>if</td>

<td>raise</td>

</tr>

<tr>

<td>None</td>

<td>del</td>

<td>import</td>

<td>return</td>

</tr>

<tr>

<td>True</td>

<td>elif</td>

<td>in</td>

<td>try</td>

</tr>

<tr>

<td>and</td>

<td>else</td>

<td>is</td>

<td>while</td>

</tr>

<tr>

<td>as</td>

<td>except</td>

<td>lambda</td>

<td>with</td>

</tr>

<tr>

<td>assert</td>

<td>finally</td>

<td>nonlocal</td>

<td>yield</td>

</tr>

<tr>

<td>break</td>

<td>for</td>

<td>not</td>

<td>await</td>

</tr>

<tr>

<td>class</td>

<td>form</td>

<td>or</td>

<td>async</td>

</tr>

<tr>

<td>continue</td>

<td>global</td>

<td>pass</td>

</tr>

</tbody>

</table>

<span id="section5"></span>

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:

<span id="section6"></span>

    FALSE

## 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).
<span id="section7"></span>
7.  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](https://realpython.com/defining-your-own-python-function/) 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](https://flexiple.com/python/python-initialize-list).