nameof()
can be used to print the name of a variable as a string, instead of its value itself:
from varname import nameof
var = 'Hello!'
print (nameof(var))
#Output:
var
The reason why I need this is because I want to print the value of each element of a list as it's being looped through:
from varname import nameof
green = 3
blue = 6
black = 9
white = 1
purple = 8
list_colors = [green, blue, black, white, purple]
for color in list_colors:
print("The color being looked at is:", nameof(color))
The output I get is:
The color being looked at is: color
The color being looked at is: color
The color being looked at is: color
The color being looked at is: color
The color being looked at is: color
But rather, I need the following as the output (and also NOT the numeric values stored in each of the variables):
The color being looked at is: green
The color being looked at is: blue
The color being looked at is: black
The color being looked at is: white
The color being looked at is: purple
Can anyone please help me with this?