Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

What are the allowed characters in Python function names?

Are some characters other than alphabets, numbers and underscores allowed in python names or python identifiers?

If yes, what are those characters?


1 Answer
Manogna

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

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.

$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.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.