如何在python中将'a = b = 2'字符串转换为a = 2,b = 2?
有一些python解析器为2 + 3 + 4等表达式提供输出。
但如何将'a = b = 2'字符串用作变量a = 2 b = 2
答案 0 :(得分:5)
s = 'a=b=2'
exec(s) #executes the string as a python command
你已经完成了
print(a)
2
print(b)
2
有关详细信息,请参阅此讨论: How do I execute a string containing Python code in Python?