How to Print a Variable’s Name in Python?

In Python, a “variable” is used to reference an object in the program. A value given to a variable can be accessed using the variable’s name. The variable’s value can be printed in Python using the built-in print() function. However, printing the variable’s name is a bit tricky.

Different approaches are used in Python to print the variable’s name, such as the “globals()” function with list comprehension, Python dictionary, etc.

This post will provide a detailed guide on printing a variable name in Python using the following content:

So, let’s get started!

Method 1: Using globals() Function

The “globals()” function is utilized along with a list comprehension method to print a variable name in Python. The globals() function retrieves the global symbol dictionary, which contains all the information related to the program. Let’s see how we can use this method to print a variable name in the below code:

Code:

int_variable = 456
int_variable1 = 46
print([name for name in globals() if globals()[name] == 456])

In the above code:

  • Two variables named “int_variable” and “int_variable1” are initialized in the program.
  • The list comprehension technique is used to iterate each item of the symbol dictionary.
  • A value is compared with the dictionary’s values. If the specified value matches any of the dictionary’s values, then the variable name in which that particular value is stored will be retrieved.

Output:

The output shows that the specified value “456” is stored in the “int_variable”.

Method 2: Using items() Method

The “items()” method is used along with the “locals()” function to print a variable name in Python. The “locals()” function returns the dictionary containing all the information related to the program in the local scope. The below example code is used to print variable names in Python:

Code:

int_variable = 76
int_variable1 = 76 print([name for name, i in locals().items() if i == 76])

In the above code:

  • The list comprehension method is used along with the “items()” function to iterate over each item and print the key that is similar to our variable value “76”.
  • The key here is the variable name that contains a similar variable value.

Output:

The above output shows that two variables contain the targeted value, i.e., “76”.

Note: Both locals() and globals() functions contain the necessary program information. One has information related to the local scope, and the other contains information related to the global scope. The below example demonstrates the comparative analysis of the locals() and globals() functions:

Code: Using locals() Function

int_variable = 76
def sample():
    int_variable1 = 76
    print([name for name, i in locals().items() if i == 76])   
sample()

In the above code, the “local()” function is used along with “items()” function to check the specific variable value “76” inside the local scope and return the variable name.

Output:

The variable name “int_variable1,” having the value “76” and residing within the local scope, is displayed on the screen.

Code: Using globals() Function

int_variable = 76
def sample():
    int_variable1 = 76
    print([name for name in globals() if globals()[name] == 76])  
sample()

In the above code, the “globals()” function is used with the list comprehension to check a specific value “76” at the global scope and return the variable name accordingly.

Output:

The globals() function retrieves the variable name with a value “76” declared at global scope.

Method 3: Using Python Dictionary

Users can manually create a dictionary that contains the variable value as a key and the variable’s name as a key’s value. Let’s understand via the following code:

Code:

val = 'Lily'
val2 = 'Alex'
name_variable = {'Lily': "val", 'Alex': "val2"}
print(name_variable['Alex'])

In the above code, two variables named “val1” and “val2” are initialized at the start of the program. The dictionary named “name_variable” is created with a key containing the variable’s names as values. The key name is passed inside the square bracket to access the variable’s name.

Output:

The above output shows that “Alex” is stored in the “val2” variable.

Conclusion

To print variable names, “globals()”, “items()”, and “Python Dictionary” methods are used in Python. The “globals()” function with the list comprehension method prints a variable name in Python. The “items()” method is also used with the “locals()” function to print a variable name in Python. Moreover, we can also define our own dictionary to print the variable name in Python. This write-up presented a comprehensive guide on how to print a variable name in Python.