How to get a Variable Name as a String in Python

In this tutorial, we will learn how to get a variable name as a string in Python. We will understand this concept with the help of an example.

Variables in Python

Variables are any values whose value is not fixed, therefore, they can be updated as per the required condition. There are two types of variables,i.e, Global variables and Local variables.

In Python, we do not require to declare the variable and the data type of the variable is also not required.

To update the value of the variable, just assign it a new value later on.

Get a Variable Name as a String

To get a variable name as a string in Python we use the iteritems() or the items() method. This is basically a process of searching for a variable name with the help of a reverse approach.

Syntax: dictionary.items()

Parameters: It is important to know that this method does not take any parameters.

Code

Let’s understand with the help of the following example:

var = 710
variable_name = [k for k, v in locals().items() if v == 710][0] 
print("Your variable name is " + variable_name)

Run this code online

Output:

Your variable name is var

Using iteritems() in Python3 produces:

AttributeError: 'dict' object has no attribute 'iteritems'

Explanation:

  • Input any variable whose name we will get as a string.
  • Use the local() to get the dictionary of the current local table.
  • Use the items() to get the variable name and print the variable name in form of a string.

I hope you were able to understand this concept. Feel free to ask any questions regarding this concept or any other topic you want to know about.

You can also learn about: Python String decode() method in Python and groups() method in Regular Expression.

5 responses to “How to get a Variable Name as a String in Python”

  1. Ruslan says:

    A small mistake in explanation.
    ‘local()’ should be changed to ‘locals()’ instead.

    • Moliver says:

      def tryme_please(**tryme):
      for key, var in tryme.items():
      print(key, type(key), var, type(var))

      tryme_please(a=2)

      🙂

  2. Sergio Pilling says:

    def print_var_name(variable):
    for name in globals():
    if eval(name) == variable:
    if eval(name) !=None:
    return(name)

    k1 = 2.1e-1
    k2 = 2.1e-2
    k3 = 2.1e-3
    k4 = 2.1e-4
    k5 = 2.1e-5
    ks_list=(k1, k2, k3, k4, k5) #tem que ter espaco entre as variaveis

    #print_var_name(k5)

    for i in ks_list:
    print(print_var_name(i), ‘=’, i)

  3. khosro says:

    Thank you so much.
    for your nice tutorial.
    Please keep writing up about python like this article.
    you save me!
    I have an idea for you which also can help people like me
    I wanna find a job in programming and break the cycles of poverty in my family. 🙂
    I’m not good at English but at least I can read this article.
    Please write simple English tutorials. for my language, There isn’t much information on the internet.

  4. cap'n says:

    There is a big issue with this method. That is, what happens if we have several variables storing the same value? If you print this out, you’ll find that order matters for the compiler when we’re searching for these variables.

    var = 710
    varrr = 710
    variable_name = [k for k, v in locals().items() if v == varrr][0]
    print(“Your variable name is ” + variable_name)

    Now try to change var and you’ll see that varrr gets printed. Very interesing…
    var = 420
    variable_name = [k for k, v in locals().items() if v == varrr][0]
    variable_name

Leave a Reply

Your email address will not be published.