Practical lesson: strings
Special characters that begin with a backslash (\) are used inside strings. \n moves the output to a new line, \t puts a tab (a wide gap), and \" and \' put a quote sign inside a string, because a plain quote would close the string. For example print("U dedi: \"Salom\"") prints U dedi: "Salom" on the screen. A string may also be wrapped in single quotes instead of double quotes, and then double quotes can be written inside it. To build shapes from characters, repeating (*) and joining (+) are used together: "*" + " " * 4 + "*" is a line with a star at each end and four spaces between. You print the lines of the shape with print() calls one after another; if needed, plan it first on squared paper.
| Sign | Meaning |
|---|---|
| \n | new line |
| \t | tab (a wide gap) |
| \" | a double quote inside a string |
| \' | a single quote inside a string |
A pupil preparing a club notice put one tab between each time and room instead of many spaces. The two columns lined up and the notice was easy to read.
Programmers, designers and text editors use characters to show lines, frames and tables neatly.
On paper write a short three-line list with a lesson name and a time on each line, and mark where you would put \t between the two parts and \n at the end of each line.
1print("Salom\nDunyo") 2print("Ism:\tAli") 3print("U dedi: \"Salom\"")
Salom Dunyo Ism: Ali U dedi: "Salom"
1print("*" * 6) 2print("*" + " " * 4 + "*") 3print("*" + " " * 4 + "*") 4print("*" * 6)
****** * * * * ******
“Character artist”: a pair plans a simple shape (a house, a tree, a star) on squared paper and prints it with several print() lines using * and +; another pair recognizes the shape on the screen.