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.


import_contacts

You may also like:

Related Posts

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

  • CharAt in Python
    08 January

    CharAt in Python

    Table of ContentsCharAt() in javaAlternative of charAt() in PythonIterate through String and print its character In this tutorial, we will see about CharAt in Python. If you are already well verse in java and might be looking for charAt() alternative in Python, then you are at right place. In Python, you can get character of […]

  • Convert dict to String in Python
    17 December

    Convert dict to string in python

    Table of ContentsHow to convert dict to string in python?Using str() method.Using the pickle module.Using the for loop.Using the json.dumps() method. In this post, we will see how to convert dict to String in Python. Python contains several data structures, and more often than not, there is a need for the conversion of one type […]

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • String to char Array in Python
    06 November

    Convert String to Char array in Python

    Table of ContentsConvert String To Char Array In Python Using For LoopConvert String To Char Array In Python Using The list() ConstructorConvert String To Char Array In Python Using The extend() MethodConvert String To Char Array In Python Using The += OperatorConvert String To Char Array In Python Using Packing And UnpackingConvert String To Char […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.