Answers for "If we need to use a variable name in a string, how do we do inside PYTHON?"

4

how to use a string variable as a variable name in python

#works only inside classes
name  = 'varname'
value = 'something'

setattr(self, name, value) #equivalent to: self.varname= 'something'

print (self.varname)
#will print 'something'
Posted by: Guest on October-10-2020

Code answers related to "If we need to use a variable name in a string, how do we do inside PYTHON?"

Python Answers by Framework

Browse Popular Code Answers by Language