What is Identifiers in Python with Example

An identifier in Python is the user-defined name given to identify a variable, function, class, string, list, dictionary, module, or another object.

It helps to differentiate one entity (or object) from another.

In Python programming language, an identifier starts with an alphabetic uppercase letter (A to Z) or lowercase letter (a to z) or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

Python does not allow us to use special characters, such as @, $, /, and % within identifiers.

As we know, Python is a case sensitive programming language. Therefore, identifiers such as ‘Variable’ and ‘variable’ are two different identifiers in Python programming language.

Five Rules for writing Identifiers in Python


There are following rules for writing identifiers in Python programming language that should keep in mind. They are:

1. Identifiers can be a combination of alphabetic letters in uppercase (A to Z) or lowercase (a to z), or digits (0 to 9) or an underscore (_). Here are some valid identifier examples in Python.

  • C
  • c
  • myClass
  • my_variable,
  • my_var1
  • UPPERCASE
  • lowercase
  • CollegeName
  • schoolName
  • UPPERCASE_WITH_UNDERSCORES
  • lowercase_with_underscores
  • max_marks

2. An identifier cannot start with a numeric digit. For example, 1person is invalid, but person1 is a valid name.

3. We cannot use keywords as identifiers. For example,

class = 10
print(class)
Output:
     File "<interactive input>", line 1 
     class = 1 
           ^ 
     SyntaxError: invalid syntax

However, we may use a trailing underscore to differentiate the name from a keyword. For example:

class_ = "RSVM"
print(class_)
Output:
      RSVM

4. Identifier may not include special symbols like !, @, #, $, % etc. For example:

[email protected] = "RSVM"
print([email protected])
Output:
    File "<interactive input>", line 1
    [email protected] = "RSVM"
            ^
    SyntaxError: invalid syntax

5. An Identifier can be of any length in Python.

Naming Conventions for Python Identifiers


Here are some naming conventions for identifiers in Python programming language. They are:

1. Class names in Python always start with an uppercase letter and all other Python identifiers begin with a lowercase letter. For example: Person.

2. Beginning an identifier with a single leading underscore (_) represents that the identifier is private. For example: _sum.

3. Beginning an identifier with two leading underscores represents the identifier is strongly private. For example: __sum.

4. If the Python identifier also ends with two trailing underscores, the identifier is a language defined special name. For example: student__.

Class Identifier in Python


A class identifier may be a combination of two or more words. All words should start with capital letters and are combined together without underscores or spaces.

For example, SchoolName, MyFamousCollection, MyCity, etc. are the valid class identifiers.


Points to Remember:

1. Python is a case-sensitive programming language. It means Python and python are not the same identifiers.

2. Always give an appropriate and meaningful identifier name that makes proper sense. For example, c = 20 is a valid identifier, but writing count = 20 would make more sense. After a long period, when you will see your code, then it would be easier to find out what it represents and its purpose.

3. An identifier may comprise several words, but for better understanding and readability, use a single underscore (_) to separate each word. For example, my_roll_no, my_great_function, HTTP_Server_Error.


Try It Yourself

Question: Which of the following are valid Python identifiers?

int45, 40ROLL, $Dollar$, printf, print, print_, _print, this, self, _person_, 0x40L, true, _, if, do, counter-1.

Answer: int45, printf, print_, _print, this, self, _person_, true, _, do are the valid identifiers.

40ROLL, $Dollar$, print, 0x40L, if, counter-1 are not valid identifiers.


In this tutorial, we have covered about Python identifiers with various examples. Hope that you will have understood all rules for writing identifiers in Python programming language.
Thanks for reading!!!
Next ⇒ Literals in Python⇐ Prev Next ⇒