python之如何執行用字符串表示的代碼

好比說如今有這麼一段字符串,"print('helloworld')",那麼怎麼將其轉換成python認識的格式運行?python

s="print('helloworld')"
r = compile(s,"<string>", "exec")
exec(r)

輸出:helloworldide

相關文章
相關標籤/搜索