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, butvariable1
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