Answers for "how to convert string to variable 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
0

how to convert variable in Python ?

Test = 12 # integer 
Converter = float(Test) # Converter = 12.0
Converter = str(Test) # Converter = "12.0"
Converter = int(Test) # Converter = 12
Posted by: Guest on February-17-2021

Code answers related to "how to convert string to variable python"

Python Answers by Framework

Browse Popular Code Answers by Language