Kirean Watkins Kirean Watkins - 1 year ago 87
Python Question

Python adding string to a variable

Is it possible to do this or something similar?

for i in range(1, 10):
x+str(i) = "hello"

Answer Source

Yes, it is, but it shouldn't be done.

If you need this feature, you use dictionaries.

And if you really insist of doing what you meant then you use the globals() or locals() dictionary:

>>> for i in range(10):
...     globals()["Var_"+str(i)] = i**2
>>> print(Var_5)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download