AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ 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.

SignMeaning
\nnew line
\ttab (a wide gap)
\"a double quote inside a string
\'a single quote inside a string
💡 Where will I use this?
In real life

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.

Who uses it

Programmers, designers and text editors use characters to show lines, frames and tables neatly.

Try it today

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.

Worked examples
Python
1print("Salom\nDunyo")
2print("Ism:\tAli")
3print("U dedi: \"Salom\"")
Output
Salom
Dunyo
Ism:	Ali
U dedi: "Salom"
Special characters – code above. The screen shows Salom and Dunyo on two lines, then Ism: followed by a tab and Ali, then U dedi: "Salom" with quotes.
Python
1print("*" * 6)
2print("*" + " " * 4 + "*")
3print("*" + " " * 4 + "*")
4print("*" * 6)
Output
******
*    *
*    *
******
A frame of stars – code above. 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?
Story of the Week · The Khwarizmi WayA photo and text for the shopClose the week: all of this week’s subjects in one storyRead →