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, name, and save string_surgery_your_turn.
- Click the Send to micro:bit button.
# 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)
- Check the results in the serial monitor.
- Verify that the original is "Have a nice day." and the modified is "Have a GREAT day!"