Is there some way to create variables in a loop in python? I am looking for something that would work like this:
for i in range(1,25):
x[i goes here] = "Hello World"
print(x5)
Thank you :)
CodePudding user response:
i would recommend using a dictionary, instead of making alot of variables something like-
vars = {}
for i in range(1, 25):
vars[f'x{i}'] = 'hello world'
print(vars['x5'])
if ur keen on making variables, do the same thing but put them in globals()
so something like this
vars = {}
for i in range(1, 25):
globals()[f'x{i}'] = 'hello world'
print(x5)