這個問題在這里已經有了答案: 如何在 Python 中執行包含 Python 代碼的字串? (14 個回答) 4 小時前關閉。
a = input('Enter something:') #e.g. input is print('Hello World')
# some function
print(a) #and then we get only `Hello World` out as we use print
我已經研究過通過 python 執行 shell 命令,但沒有什么與我想做的事情相關。我需要更改為不是 python 的語言嗎?甚至有可能做到嗎?
uj5u.com熱心網友回復:
對于陳述句,使用exec(string)(Python 2/3) 或exec string(Python 2):
因此,與其列印a它,不如使用exec
順便說一句,如果您打算在任何軟體中按原樣使用它,我會說這是非常不安全的。
uj5u.com熱心網友回復:
您可以使用exec:
>>> a = input("Enter something: ")
Enter something: print("hello world")
>>> exec(a)
hello world
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/331640.html
上一篇:考慮使用items()進行迭代
