☰ Contents · Computing

String practice and input and output methods

Lessons 30–31 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
30

Practical lesson: strings

Textbook: p. 67
GoalUses the special characters in strings (\n, \t, \", \'), writes quotes inside quotes correctly, and builds simple shapes from characters using * and +.
New words
\n: moves the output to a new line · \n\t: a tab, a wide gap in a line · \t\" and \': a quote sign placed inside a string · \"building a shape: lines of characters made with * and + operations · shakl yasash
Explanation

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.

Worked examples
Special characters: print("Salom\nDunyo") ⏎ print("Ism:\tAli") ⏎ print("U dedi: \"Salom\"") The screen shows Salom and Dunyo on two lines, then Ism: followed by a tab and Ali, then U dedi: "Salom" with quotes.
A frame of stars: print("*" * 6) ⏎ print("*" + " " * 4 + "*") ⏎ print("*" + " " * 4 + "*") ⏎ print("*" * 6) 6 stars, two middle lines and another 6 stars make a rectangular frame.
Class activity

“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.

Practice
1
How does print("A\tB\nC") look on the screen?
2
Write a program that prints a rectangle of the “#” sign with 2 rows of 7 signs each.
3
Print an upside-down 3-row triangle of stars (3, 2, 1 stars).
4
Why does print("U dedi: "Salom"") give an error, and how is it fixed?