Table of Contents
In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how to get a variable name as a string in Python.
What are variable names in Python?
The term variable
can be defined as a vessel that holds or stores data values in Python. Variables can either be local
or Global
depending on their definition.
The declaration of the variable is not essential in Python and values can just be updated by assigning new ones in the code.
How to get a variable name as a string in Python?
The items()
or the iteritems()
function can be utilized to get the variable name in the form of a string in Python. However, it is essential to discover the string of the variable name in order to extricate it.
This is a tweak to an original process, which makes the code reverse search in order to find the variable name.
In this method, we will use the iteritems()
or the items()
function, which is utilized to provide a list of all existing variables in the given scope along with the values that they are holding as the output.
In Python 2.x
The following example makes use of the iteritems()
function to get a variable name as a string in Python.
1 2 3 4 5 6 |
# Python 2 code x = 7 x = [ i for i, a in locals().iteritems() if a == x][0] print("The variable name:", x) |
The above code provides the following output:
Explanation:
- We will first take any variable which we have to ultimately work on to get in the form of a string.
- The
locals()
function is then utilized to get the details of the given local table in the form of a Pythondictionary
. - Finally, within the
locals()
function, we will use theitems()
or theiteritems()
function depending on the compiler version to receive and print the name of the variable as a string in the output.
We should note that the above code would run perfectly on the versions Python 2.x
, but will provide an error when run in any of the versions of Python 3
.
Using this code in Python 3 provides the following error message.
This is due to the fact that the iteritems()
function was omitted in the newer versions of python (Python 3.x)
. Instead of this, the items()
function can be utilized in newer versions of Python and it will help generate the same results.
In Python 3.x
The following example makes use of the items()
function to get a variable name as a string in Python 3.x
.
1 2 3 4 5 6 |
# Python 3 code x = 7 x = [ i for i, a in locals().items() if a == x][0] print("The variable name:", x) |
The above code provides the following output:
In the end, I would like to infer that even though it is possible to get the variable name as a string in Python, it should not be your go-to in every scenario. A variable
is not one of the objects that can have canonical names.
On top of that, the names of the variables can be altered or changed anytime during the run of the code, which leads to an uncertainty in the correctness and preciseness of the output.
That’s all about how to get variable name as String in python.