Skip to main content

Python Identifiers

It is a name given to entities like class, variables, module or other object. The identifier is a combination of character digits and underscore. The identifier should start with a character or Underscore then use digit. It starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

Python does not allow punctuation characters such as @, $, and % within identifiers. Python is a case sensitive programming language. 

Rules for naming conventions for Python identifiers:-

  • Except class name all other identifiers starts with a lowercase letter.
  • An identifier cannot start with a digit. 1variable is invalid, but variable1 is a valid name.
  • Starting an identifier with a single leading underscore indicates that the identifier is private.
  • Starting an identifier with two leading underscores indicates a strongly private identifier.
  • An identifier can be of any length.
  • Keywords cannot be used as identifiers.
global = 12

Output:

File "<interactive input>", line 1

        global=1
          ^
SyntaxError: invalid syntax
  • We cannot use special symbols like !@#$% etc. in our identifier.
a@=0

Output:

 File "<interactive input>", line 1
    a@ = 0
     ^
SyntaxError: invalid syntax

Example of correct identifiers

var1
_var1
_1_var
var_1

Example of incorrect identifiers

!var1
1var
1_var
var#1
Tags
Submitted by devanshi.srivastava on June 4, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.