Answers for "create dynamic variable in python"

4

how to create dynamic variable names in python

for x in range(0, 9):
    globals()['string%s' % x] = 'Hello'
# string0 = 'Hello', string1 = 'Hello' ... string8 = 'Hello'
Posted by: Guest on September-03-2020
0

can we make dynamic variable in pytohn?

# Yes, globals() gives excess to all the global variables.
# globals() is a dictionary that has 'variable' : 'value' as key, value pairs...
# You may use something like

for i in range(100):
	globals()['variableno_{1}'.format(i)] = "My name is variableno_{1}".format(i)
    
 # this loop creates 100 different variable in global scope same like assigning...
# variableno_0 = "My name is variableno_1"
# variableno_1 = "My name is variableno_2"
# variableno_2 = "My name is variableno_3"
....
....
# variableno_97 = "My name is variableno_97"
# variableno_98 = "My name is variableno_98"
# variableno_99 = "My name is variableno_99"
Posted by: Guest on April-02-2021
-1

creating dynamic variable in python

for i in range(0, 9):
Posted by: Guest on February-25-2021

Code answers related to "create dynamic variable in python"

Python Answers by Framework

Browse Popular Code Answers by Language