Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: print variable name and value easily

I want to use a function that can automatically print out the variable and the value. Like shown below:

num = 3
coolprint(num)

output:

num = 3

furthermore, it would be cool if it could also do something like this:

variable.a = 3
variable.b = 5
coolprint(vars(variable))

output:

vars(variable) = {'a': 3, 'b': 5}

Is there any function like this already out there? Or should I make my own? Thanks

like image 469
Slaknation Avatar asked Sep 06 '17 18:09

Slaknation


People also ask

How do you print a variable name in Python?

Printing variables using f-string:Braces {} are placed around the names of variables that you are looking to print. Python replaces these variables with their values when the code is executed and the string is displayed. Such strings are called f-strings.

How do I print the data type of a variable or a value?

To get the type of a variable in Python, you can use the built-in type() function. In Python, everything is an object. So, when you use the type() function to print the type of the value stored in a variable to the console, it returns the class type of the object.

How do you get the value of a variable in Python?

Python variables store values in a program. You can refer to the name of a variable to access its value. The value of a variable can be changed throughout your program. Variables are declared using this syntax: name = value.

How do you print a variable in a string in Python?

You first include the character f before the opening and closing quotation marks, inside the print () function. To print a variable with a string in one line, you again include the character f in the same place – right before the quotation marks.

What is the use of print statement in Python?

As mentioned above, the print statement is used to output all kinds of information. This includes textual and numerical data,variables, and other data types. You can also print text (or strings) combined with variables, all in one statement.

How to print string values besides the variables in the command?

The , command operator is used to printing multiple variables by providing them into print () statement as parameters. The variables are joined and printed as a single string. name = "Pythontect" number=123 print ("Hello ",name,number) We can also print string values besides the variables like below by using the print () statement.

How to print Unicode text with variable in Python?

All text is Unicode; however encoded Unicode is represented as binary data. The handling of variable is different with Python so if you wish to print text with variable then you need to use proper syntax with print () function. If you only want to print the variable having integer value without any other content


2 Answers

From Python 3.8 there is a = for f-strings:

#!/usr/bin/env python3
python="rocks"
print(f"{python=}")

This would output

# python=rocks
like image 141
The Unfun Cat Avatar answered Nov 03 '22 01:11

The Unfun Cat


This lambda-based solution works well enough for me, though perhaps not in every case. It is very simple and only consumes one line.

coolprint = lambda *w: [print(x,'=',eval(x)) for x in w]

Exmaple..

coolprint = lambda *w: [print(x,'=',eval(x)) for x in w]

a, *b, c = [1, 2, 3, 4, 5]

coolprint('a')
coolprint('b','c')
coolprint('a','b','c')
coolprint('c','b','b','a','b','c')

which produces..

a = 1
b = [2, 3, 4]
c = 5
a = 1
b = [2, 3, 4]
c = 5
a = 1
b = [2, 3, 4]
c = 5
c = 5
b = [2, 3, 4]
b = [2, 3, 4]
a = 1
b = [2, 3, 4]
c = 5
like image 26
Paperclip Bob Avatar answered Nov 02 '22 23:11

Paperclip Bob