Strings can contain Python expressions, or even one or more Python statements. So long as those strings are passed to the correct functions, an incoming string can be evaluated as an expression or even executed as one or more Python statements!
Security heads up! If you are going to set up an app that can run scripts you can type in, you will also have to make sure that only you and others with permission can send text with embedded scripts to your app!Let's start with evaluating an expression that's in a string.
# embed_intro from microbit import * sleep(1000) s = "1 + 2 + 3 + 4" reps = eval(s) print("reps = ", reps)
The script starts with the string s = "1 + 2 + 3 + 4". Then, eval(s) evaluates 1 + 2 + 3 + 4 as a Python expression and returns 10. Since reps = eval("1 + 2 + 3 + 4"), the result of 10 is stored as an int in reps.
While Python's built-in eval() function is for evaluating expressions embedded in strings, its exec() function can actually execute Python statements.
Remember that a string that starts and ends with triple-quotes """ can span multiple lines!
Let's try adding statements contained in a string, and then executing them with the exec() function.
# embed_try_this from microbit import * sleep(1000) s = "1 + 2 + 3 + 4" reps = eval(s) print("reps = ", reps) s = """ # <- add print('Start counting:') # <- add for n in range(0, reps): # <- add print('n = ', n) # <- add """ # <- add exec(s) # <- add print('Done!') # <- add
A script that can run Python scripts embedded in strings opens up a lot of possibilities. Cybersecurity activities rely heavily on a computer-connected micro:bit that radio-transmits strings with commands to a remote cyber:bot. Since scripts can be embedded in strings, it might be possible for you to update your cyber:bot robot's script on the fly! One challenge will be to prevent hackers from doing that to your cyber:bot for their own reasons!
This simple script accepts Python statements that you enter. After you have entered the last Python statement, type run and press Enter to execute the script.
# embed_your_turn from microbit import * sleep(1000) s = "" t = "" while True: s = input("> ") if(s != 'run'): t = t + s + "\n" else: exec(t) t = ""
Did each different script you entered execute in the terminal?
The script starts by creating empty strings named s and script. The s string receives each line you type, and the script string stores the actual script consisting of all the statements you type.
s = "" script = ""
In the main loop, the s variable prompts for input from you by displaying a > prompt. When you type a Python statement and press Enter, that statement gets stored in s.
while True:
s = input("> ")
The if...else… statement checks to see if what you typed does not match the string "run". When it does not match, it assumes you have typed a statement and uses script = script + s + "\n" to append your statement and add a newline character to any previous statements you might have typed.
if(s != 'run'): script = script + s + "\n"
When you do type run and press Enter, the exec(script) function call runs the Python script you typed. Then script = "" sets the script variable to an empty string so that you are starting over with an empty script.
else: exec(script) script = ""