How To Print A Variable’s Name In Python

For easy control of programs, developers may need to print a variable’s name in Python. Fortunately, Python comes up with many built-in features that help you do this job. Today, we will go together to learn all the ways to print out the variable name on the screen. Let’s get started!

How to print a variable’s name in Python

Below are the easiest ways to print a variable’s name in Python:

Using the print() function

The most common way is that you put the variable’s name as a string and then use the function print() to print it out. The string name of the variable will be right next to the value that you want to print.

In other words, the command you use is:

print("[variable] =", [variable])

You fill the variable name in both 2 parameters in the print() function. But in the first parameter, you place the variable name in the "" or '' to describe it as a string. The other parameter is just the variable name to print its value.

See the sample below:

# Variable name is "varName", value is "learnshareit.com"
varName = "learnshareit.com"
print("varName = ", varName)

The output will be:

varName =  learnshareit.com

Of course, the value is not necessary for string. It can be any data type, such as int, float, list, etc.

See the sample below:

# Variable as an int
varInt = 1
print("varInt = ", varInt)

# Variable as a float
varFlt = 1.0
print("varFlt = ", varFlt)

# Variable as a list
varLst = [6, 3.5, "string"]
print("varLst = ", varLst)

The output will be:


varInt =  1
varFlt =  1.0
varLst =  [6, 3.5, 'string']

Using f-str feature

Another way is to use the f-str feature in Python. You combine this feature with the print() function to print a variable’s name in Python. Here is the command you can use”:

print(f'{[variable] = }')

As you see, you just need to write the variable once in the {}. The output will include both the variable’s name and value.

See how you write the code here:

varName = "learnshareit.com"

# Using the f-str feature
print(f'{varName = }')

The output will be:

varName = 'learnshareit.com'

Of course, there is no limitation for the data type of the variable. The value can be string, float, int, list, and so on!

Here is the sample:

# Variable as an int
varInt = 1
print(f'{varInt = }')

# Variable as a float
varFlt = 1.0
print(f'{varFlt = }')

# Variable as a list
varLst = [6, 3.5, "string"]
print(f'{varLst = }')

The output will be:

varInt = 1
varFlt = 1.0
varLst = [6, 3.5, 'string']

Using the enum module

You can also use the enum module to print a variable’s name in Python. This module includes the feature Enum that helps you do this job. Make sure that you import it in the beginning of your Python program using the following command:

from enum import Enum

You create your variables and assign values to them in the following commands:

class Type(Enum):

    [variable] = [value]

    [variable] = [value]

    ...

The class Type(Enum) allows you to store many different variables. Each variable will have its own value. There is no limitation to the data type you can use.

To print the variable’s name, you will combine the method Type.[variable].[parameter] with the print() function.

The parameter can be a name or a value, depending on what you want to print out. The sample commands are like so:

To print the name of the variable, write:

print(Type.[variable].name)

To print the value of the variable, write:

print(Type.[variable].value)

Here is the code sample:

from enum import Enum

# Create the class Type(Enum) to store many variables
class Type(Enum):    
  varName = "learnshareit.net"    
  varInt = 1

# Print the name and value of the variables
print(Type.varName.name, "=", Type.varName.value)
print(Type.varInt.name, "=", Type.varInt.value)

The output will be:

varName = learnshareit.net
varInt = 1

Summary

Above are the top ways to print a variable’s name in Python. Overall, using the print() function is the easiest way. But the other 2 are also good to refer to. You will be the one who decides the way that is the most convenient for your Python programming!

Leave a Reply

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