Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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'
Comment

python use variable name as string

>>> foo = dict()
>>> f'{foo=}'.split('=')[0]
'foo'
Comment

PREVIOUS NEXT
Code Example
Python :: delete directory if exists python 
Python :: python convert string datetime into datetime 
Python :: python ssl module is not available 
Python :: get a slice of string in python 
Python :: select rows where column value is in list of values 
Python :: django orm count 
Python :: python how to split a number 
Python :: what is // in python 
Python :: mouse bottom in pygame 
Python :: discord py bot example 
Python :: create new list in for loop python 
Python :: pandas gropu by 
Python :: how to create a tuple from csv python 
Python :: correlation analysis of dataframe python 
Python :: python how to convert csv to array 
Python :: unshorten url python 
Python :: twin axis python 
Python :: assert keyword python 
Python :: # invert a dictionary 
Python :: ip condition in tpl 
Python :: keras example 
Python :: convert decimal to hex python 
Python :: python merge lists 
Python :: Create list with numbers between 2 values 
Python :: two for loops in list comprehension 
Python :: if elseif in single line python 
Python :: pandas strip whitespace 
Python :: python count bits 
Python :: remove punctuation python 
Python :: use a dictionary to make a column of values 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =