-1

I want to create variable names using for loop in python,

I tried the code below:

for i in range(5):
    var+str(i)=i
    print(var+str)

but it returns a error:

  File "<stdin>", line 2
SyntaxError: cannot assign to operator

Can Anyone help me this that where i go wrong or there is any other method for this?

khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

1

You should use a python dictionary.

d = {}
for x in range(1, 3):
    d["string{0}".format(x)] = "Foo"

>>> d["string5"]
'Foo'
>>> d
{'string1': 'Hello',
 'string2': 'Hello',
 'string3': 'Hello',
}