Let's start with a script that creates a string, names it s, and prints it.
# first_string_intro from microbit import * sleep(1000) s = "ABC 123" print(s)
The statement s = "ABC 123" creates a string variable named s. The string it refers to contains the characters A B C 1 2 3. The statement print(s) displays the contents of the string named s.
A print statement allows you to display more than one string at a time. You will find it helpful to add a string that explains what's about to be printed. So, instead of just print(s), you could use print("s = ", s). This will become important when you write larger scripts. While adjusting them to work the way you want, you can include information such as the variable name and the location within the the script the print statement is being executed. Here is an example:
s = "ABC 123" print("s =", s)
As mentioned earlier, strings can contain multiple lines.
"""This is also a string with more than one line enclosed by three double-quotes."""
A single line string can also be split up into multiple lines in the Python script.
"This is a string with only one line"\ "that has been split into multiple"\ "lines to fit in your code editor"\
A string in double quotes can contain single quotes:
"This string in double-quotes 'contains' single-quotes."
Strings can also contain escape characters preceded by a backslash to print special-case characters like the tab, apostrophe, and newline. Here’s an example you will try.
s4 = 's4 has tab \t apostrophe \', and newline \n...for next line.'
Keep in mind that if the above string was enclosed in double quotes, it would not need the \' to display the apostrophe.
Let's try modifying the script so that it displays s = ABC 123 instead of just ABC 123 in the terminal. That way, you'll know that the terminal is displaying the contents of the s variable you created. This is very useful in a script that has many variables!
# first_string_try_this from microbit import * sleep(1000) s = "ABC 123" print("s =", s)
This script has some of the fancier strings that were discussed earlier. s1 is multiline, s2 is a single line that has been split across multiple lines in the Python editor. S3 is a string in double-quotes that contains a couple of single quotes. S4 has three escape characters, \t for tab, \' for a single quote, and \n for the invisible character that advances the cursor to the next line.
# first_string_your_turn from microbit import * sleep(1000) s1 = """s1 is a string with more than one line enclosed by three double-quotes.""" s2 = "s2 is a string with only one line "\ "that has been split into multiple "\ "lines to fit in your code editor. "\ s3 = "s3 is in double-quotes but 'contains' single-quotes." s4 = 's4 has tab \t apostrophe \', and newline \n...for next line.' print(s1) print(s2) print(s3) print(s4)
Keep in mind that some of the lines might wrap depending on the width of the browser and possibly due to settings inside the terminal.
Links
[1] https://python.microbit.org/v/2