I want to do something like this in Python:
a_string = 'variable_name'
print('{0}'.format(variable_name))
That is, I want to extract a variable's name from the string and send it into .format(). How can I do?
I want to do something like this in Python:
a_string = 'variable_name'
print('{0}'.format(variable_name))
That is, I want to extract a variable's name from the string and send it into .format(). How can I do?
This, in general, is not considered a good practice - data should be data, and code should be code. There are, of course, exceptions to that general rule.
To do what you want, you can simply access the dictionary returned
by a call to vars:
a_string = 'variable_name'
print('{0}'.format(vars()[variable_name]))
vars checks the variable availability in the local context (the same as a call to locals())
Back to good practices: if you need strings to locate data in your program, you should check if you could not be using a dictionary instead of letting this data, with dynamic name, directly in a variable. One of the main purposes of dictionaries is allowing dynamic look-up of data.