Computer Science, asked by kawtharshafa9372, 1 year ago

How do I execute a string containing Python code in Python?

Answers

Answered by omegads04
0

mycode = 'print("hello world")' code = """ def mutiply(x,y): return x*y print('Multiply of 2 and 3 is: ',mutiply (2,3)) """ exec(mycode) exec(code) .

Answered by fiercespartan
0

I don't get quite what you want to ask but here is my thought:

How do I execute a string given in an input which contains a operation in python.

For this, python added a cool new feature in 3.7

The command is called eval()

For example, your code is:

eval(input())

Your input is print (2+3) then, your code will be giving an output of 5.

Here is a image showing the input and output:

Hope it helps!

Attachments:
Similar questions