How do you print a variable name as a string in python?
TL;DRUse the Show
For list comprehension part, you can do:
I am the author of the The long answerIs it even possible?Yes and No. We are retrieving the variable names at runtime, so we need a function to be called to enable us to access the previous frames to retrieve the variable names. That's why we need a However, the source code/AST nodes in the previous frames are not always available, or they could be modified by other environments (e.g: How to do it?First of all, we need to identify which frame the variable is given. It's not always simply the direct previous frame. For example, we may have another wrapper for the function:
In the above example, we have to skip the frame inside Then we need to parse the AST node to locate where the variable is assigned value (function call) to. It's not always just a simple assignment. Sometimes there could be complex AST nodes, for example, How reliable is it?Once we identify the assignment node, it is reliable.
It partially works with environments where other AST magics apply, including pytest, ipython, macropy, birdseye, reticulate with R, etc. Neither executing nor varname is 100% working with those environments. Do we need a package to do it?Well, yes and no, again. If your scenario is simple, the code provided by @juan Isaza or @scohe001 probably is enough for you to work with the case where a variable is defined at the direct previous frame and the AST node is a simple assignment. You just need to go one frame back and retrieve the information there. However, if the scenario becomes complicated, or we need to adopt different application scenarios, you probably need a package like
How about the Like the answer provided by @Aivar Paalberg. It's definitely fast and reliable. However, it's not at runtime, meaning that you have to know it's
Finally
Read more from its documentation. However, the final word I want to say is that, try to avoid using it whenever you can. Because you can't make sure that the client code will run in an environment where the source node is available or AST node is accessible. And of course, it costs resources to parse the source code, identify the environment, retrieve the AST nodes and evaluate them when needed. How do you print a variable in a string in Python?Use an f-string to print a variable with a string
Within a print() statement, add f before a string literal and insert {var} within the string literal to print the string literal with the variable var inserted at the specified location. print(f"There are {a_variable} people coming.")
How do you name a variable in a string in Python?String Into Variable Name in Python Using the vars() Function. Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.
Can you print variable name in Python?You can't, as there are no variables in Python but only names.
How do you print a string name in Python?The closest you can get is print('my name is {name}'. format(name=my_dict['name'])) .
|