In cybersecurity situations, your script might need to modify a string it receives. One method for modifying strings is to create a larger string by adding smaller strings together. This process is called concatenation.
# string_surgery_intro from microbit import * sleep(1000) s1 = "Have " s2 = "a " s3 = "nice " s4 = "day." s = s1 + s2 + s3 + s4 print("s = ", s)
The script starts with 4 separate strings: s1, s2, s3, and s4, and addes them together. Addition with string objects is very different from the addition in the int and float objects you are probably familiar with. When string objects are added, they are combined. So, "abc" + "def" results in a string "abcdef". In our case, s = s1 + s2 + s3 + s4 results in a new string variable s that refers to the "Have a nice day." string.
In the previous activity, a single character was accessed using an index, like c = s[3]. Your scripts can also view segments of strings by using a range instead of a single index number in the square brackets.
Let's say you have a string named s:
In Python-speak, strings are considered immutable. Being immutable means that once created, a string cannot be changed. That doesn't mean that s = "Have a nice day." cannot be changed to s = "Have a GREAT day!" It just means that the original "Have a nice day." string cannot be changed. A statement can still grab parts of "Have a nice day." and use them to create a new string that reads "Have a GREAT day!" The resulting string can even be assigned back to s.
s = "Have a nice day." # BEFORE s = s[:7] + "GREAT" + s[11:15] + "!" s = "Have a GREAT day!" # AFTER
The s variable starts as this string: "Have a nice day." The second line adds "Have a " + "GREAT" + " day" + "!". See how "Have a " is s[:7] from the original string? Add the string "GREAT" to that, and then " day" with a leading space, which is s[11:15], and lastly add "!", and the string surgery is complete!
This script starts with "Have a nice day." and then creates a new string sn with the "nice" portion of the original. After that, it demonstrates some more ways to access segments of an original string that were introduced above.
# string_surgery_try_this from microbit import * sleep(1000) s = "Have a nice day!" print("Original: s = ", s) sn = s[7:11] print("Create sn from s[7:11]") print("sn =", sn) print("More substrings:") print("s[5] = ", s[5]) print("s[:7] = ", s[:7]) print("s[7:10] = ", s[7:11]) print("s[10:] = ", s[11:])
The original string: s = Have a nice day!
Create sn from s[7:11] sn = nice
More substrings:
s[5] = a
s[:7] = Have a
s[7:10] = nice
s[10:] = day!
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.
# 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)