Python之eval和exec

eval能夠執行字符串形式的表達式ide

In [1]: eval("1 + 2 + 3")
Out[1]: 6

In [2]: eval("a + 1", {"a": 99})
Out[2]: 100

 

exec執行字符串形式的代碼spa

In [3]: exec("print('hello world')")
hello world

In [4]: exec("print("hello world")")
  File "<string>", line 1
    print(「hello world「)
               ^
SyntaxError: invalid character in identifier

內外層雙引號不能相同,不然會報錯code

相關文章
相關標籤/搜索