Open In App

What are the allowed characters in Python function names?

Last Updated : 22 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python3




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


 

 

Output:

 

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

 

Python3




# 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')
   
my_function%()


Output:

File "/home/3ae3b1299ee9c1c04566e45e98b13791.py", 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.

Python3




# 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)


Output:

2020
This is a string QWERTY


Previous Article
Next Article

Similar Reads

Pandas - Remove special characters from column names
Let us see how to remove special characters like #, @, &, etc. from column names in the pandas data frame. Here we will use replace function for removing special character. Example 1: remove a special character from column names Python Code # import pandas import pandas as pd # create data frame Data = {'Name#': ['Mukul', 'Rohan', 'Mayank', 'Sh
1 min read
Python Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i]. Examples :   Input: arr[] = {1, 20, 2, 10} Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 Input: arr[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9} Outp
3 min read
Python | Pandas MultiIndex.names
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas MultiIndex.names attribute returns the names of levels in the MultiIndex. Syntax: MultiIndex.names Example #1: Use MultiIndex.nam
2 min read
Python | Pandas TimedeltaIndex.names
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.names attribute return a frozen list containing the names of the TimedeltaIndex object if it is set. If no name is
2 min read
Rename all file names in your directory using Python
Given multiple files in a directory having different names, the task is to rename all those files in sorted order. We can use OS module in order to do this operation. The OS module in Python provides functions for interacting with the operating system and provides a portable way of using operating system-dependent functionality. We can go to the cu
1 min read
Python IMDbPY - Getting alternate names of person
In this article we will see how we can get the alternate names of the person from the person object, alternate names are basically nick names given to them or stage name i.e name by which they are called, imdb person object act similar to dictionary therefore getting image from person object is similar from getting required information from the dic
2 min read
Python IMDbPY – Getting alternate names of the series
In this article we will see how we can get the alternate names(akas) of the series. Alternate names are given to many series as in different language words mean different therefore alternate names are given to each series. In order to get this we have to do the following - 1. Get the series details with the help of get_movie method 2. As this objec
2 min read
Get the city, state, and country names from latitude and longitude using Python
In this article, we are going to write a python script to get the city, state, and country names by using latitude and longitude using the Geopy module.geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the world. To install the Geopy module, run the following command in your te
2 min read
Create a GUI to find the IP for Domain names using Python
Prerequisite: Python GUI – tkinter In this article, we are going to see how to find IP from Domain Names and bind it with GUI Application using Python. We will use iplookup module for looking up IP from Domain Names. It is a small module which accepts a single domain as a string, or multiple domains as a list, and returns a list of associated IP. R
2 min read
Get column names from CSV using Python
CSV stands for Comma Separated Values and CSV files are essentially text files which are used to store data in a tabular fashion using commas (,) as delimiters. CSV is a file format and all the files of this format are stored with a .csv extension. It is a very popular and extensively used format for storing the data in a structured form. CSV files
3 min read
Practice Tags :