Open In App
Related Articles

What are the allowed characters in Python function names?

Improve Article
Save Article
Like Article

The user-defined names that are given to Functions or variables are known as Identifiers. It helps in differentiating one entity from another and also serves as a definition of the use of that entity sometimes. As in every programming language, there are some restrictions/ limitations for Identifiers. So, is the case with Python, we need to take care of the following points before using an Identifier.

Rules for writing Identifiers:

  • The first and foremost restriction is that Identifiers cannot be the same as Keywords. There are special reserved keywords in every programming language that has its own meaning and these names can’t be used as Identifiers in Python.


# Python program to demonstrate
# that keywords cant be used as
# identifiers
def calculate_sum(a, b):
  return a + b
x = 2
y = 5
# def and if is a keyword, so
# this would give invalid
# syntax error
def = 12  
if = 2     
print(calculate_sum(def, if))





 File "/home/", line 15
    def = 12   
SyntaxError: invalid syntax
  • An identifier in Python cannot use any special symbols like !, @, #, $, % etc.



# Python code to demonstrate
# that we can't use special
# character like !,@,#,$,%.etc
# as identifier
# valid identifier
var1 = 46 
var2 = 23 
print(var1 * var2)
# invalid identifier,
# will give invalid syntax error
var@ = 12 
$var = 55 
print(var@ * $var)
# even function names can't
# have special characters
def my_function%():
  print('This is a function with invalid identifier')


File "/home/", line 13
    var@ = 12  
SyntaxError: invalid syntax
  • Apart from these restrictions, Python allows Identifiers to be a combination of lowercase letters (a to z) or uppercase letters (A to Z) or digits (0 to 9) or an underscore (_). But variable name must not be started with digits. Names like myClass, var_3, and print_to_screen, are valid examples.


# Python program to demonstrate
# some examples of valid identifiers
var1 = 101
ABC = "This is a string"
fr12 = 20
x_y = 'GfG'
slp__72 = ' QWERTY'
print(var1 * fr12)
print(ABC + slp__72)


This is a string QWERTY

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 22 Jun, 2021
Like Article
Save Article
Similar Reads
Related Tutorials