Declare a string a variable in python -


this question has answer here:

my code has variables mri1_tasks , mri2_tasks. there function have python interpret 'mri%d_tasks'%num (where num equal either 1 or 2) stored value of mri1_tasks or mri2_tasks based on value of num?

instead of having 2 variables, can store variables in dictionary, variable name key.

vars = {     'mri1_tasks': 0,     'mri2_tasks': 1, }  num = 1 print vars['mri%d_tasks'%num] 

Comments