Your Turn: Create a Modified String

Now that we can concatenate strings with + and access substrings, let's make a script that starts with "Have a nice day." and uses parts of it to create "Have a GREAT day!"  It's the equivalent of substituting "GREAT" in place of "nice" and "!" in place of "."

As you work with this script, keep in mind that the original string "Have a nice day." was never changed.  A new string was created using parts of the original and then assigned equal to s, redefining s.  

Example script: string_surgery_your_turn

  • Enter this script and save it as string_surgery_your_turn.  
  • Flash the script into the micro:bit.
# string_surgery_your_turn

from microbit import *

sleep(1000)

s = "Have a nice day."

print("Original string: s = ", s)

s = s[:7] + "GREAT" + s[11:15] + "!"

print("Modified string: s = ", s)
  • Open the terminal.
  • Verify that the original is "Have a nice day." and the modified is "Have a GREAT day!"