-2

I have a list of variables in Python with below values.

I am trying to print the values of these variables in the following manner, but this is unsuccessful. Can someone please help with a solution ?

a1=1
a2=2
a3=3
for i in range(1,4):
  temp="a"+str(i)
  print(temp)

I want the output in 'temp' print the values(viz) 1,2,3 whereas the output seen are the variables (viz) a1,a2,a3

GAuto
  • 149
  • 1
  • 1
  • 5

1 Answers1

1

Try This:-

a1=1
a2=2
a3=3
for i in range(1,4):
  temp="a"+str(i)
  print locals()[temp]
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30