How to get variable name as String in Python

Get variable name as String in Python

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.

The above code provides the following output:

The variable name: x
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 Python dictionary.
  • Finally, within the locals() function, we will use the items() or the iteritems() 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.

AttributeError: ‘dict’ object has no attribute ‘iteritems’

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.

The above code provides the following output:

The variable name: x

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.