0

How to convert a=b=2 string into a=2, b=2 (actual assignments) in python?

There are some python parsers which give output for expressions like 2+3+4.

But how to use a=b=2 string into variable as a=2 b=2?

oguz ismail
  • 1
  • 16
  • 47
  • 69
Girish Ns
  • 281
  • 1
  • 3
  • 8

1 Answers1

5
s = 'a=b=2'
exec(s)    #executes the string as a python command

And you are done

print(a)
2
print(b)
2

Refer to this discussion for more info: How do I execute a string containing Python code in Python?

Community
  • 1
  • 1
Vishal
  • 3,178
  • 2
  • 34
  • 47