What are the allowed characters in Python function names?

PythonServer Side ProgrammingProgramming

Python Identifiers

Identifier is the name given to entities like class, functions, variables etc. in Python. It helps in knowing one entity from another.

Rules for writing identifiers

Identifiers can be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore (_). Names like myClass, var_3 and print_to_screen, all are valid examples.

An identifier cannot start with a digit. 2variable is invalid, but variable2 is perfectly correct.

Keywords cannot be used as identifiers. The word ‘global’ is a keyword in python. So we get an invalid syntax error here

Example

global = "syntex"
print global

Output

File "identifiers1.py", line 3
    global = "syntex"
           ^
SyntaxError: invalid syntax

Explanation:

The above code when run shows error because keyword global is used

as a variable/identifier for assigning a string value.

We cannot use special symbols like !, @, #, $, % etc. in our identifier.

Example

$local = 5
print $local

Output

File "identifiers2.py", line 1
    $local = 5
    ^
SyntaxError: invalid syntax

Explanation:

The above code when run shows error because special character $ is used in the variable/identifier for assigning a integer value.

raja
Published on 07-Dec-2017 17:36:39
Advertisements