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
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.
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.
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.
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.
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.
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.
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
From Python 3.8 there is a = for f-strings:
#!/usr/bin/env python3
python="rocks"
print(f"{python=}")
This would output
# python=rocks
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With