How to execute a string containing code in Python

1 Answer

0 votes
codestring = 'x = 7\ny=12\nsum=x+y\nprint("sum =",sum)'

result = compile(codestring, 'file name - not read from a file', 'exec')

exec(result)

 
 
 
'''
run:
 
sum = 19
 
'''

 



answered Apr 25, 2021 by avibootz

Related questions

...