Kirean Watkins Kirean Watkins - 6 months ago 8
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

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)
25