Lesson 9
Special characters: \\ \" \n
After this lesson: You'll print a backslash, a quote inside text and a new line - using special characters.
Video
The video for this lesson is coming soon.
Explanation
You can already draw houses, trees and stairs with print. Today we'll draw a kitten with the ears /\_/\ - but for that you need a secret: special characters! ๐ฑ
The backslash - a special character
The backslash \ is a special character inside text. Python reads it together with the next character and treats those two characters as one:
| In the code | On the screen |
|---|---|
\\ | \ - one backslash |
\" | " - a quote inside the text |
\n | a new line (Enter) |
To print one backslash we write two in the code: \\. The ordinary slash / isn't special - we write it as usual.
Bajtek builds a house of cards ๐ฌ
Click Next to go step by step.
Two cards
Bajtek is building a house of cards ๐. Two cards leaning on each other look like this: /\. The left card is an ordinary slash /, and the right one is a backslash \.
Character by character: \\
Top - what's in the code. Bottom - what appears on the screen. The orange box is the pair \\, which turns into one backslash.
print("\\")
In the code
characters in the code: 2, on the screen: 1
Screen
\
The simplest example: one backslash
Now the same backslash in a program: in the code we write it twice, and the screen shows one. Click the arrow!
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("\\")
โCharacter by character: /\\
The ordinary / goes to the screen unchanged, and the pair \\ turns into one backslash.
print("/\\")
In the code
characters in the code: 3, on the screen: 2
Screen
/\
A bit harder: /\\
Now an ordinary slash / and a backslash \ side by side. The ordinary / is written as usual, and \ twice. That's what the kitten's ears will look like!
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("/\\")2print("/\\/\\")
โA kitten - with a surprise
Now the whole kitten in a program. In the code we write two backslashes \\, and the screen shows one \. Compare the code with the screen!
Click the arrow - every print draws one more line of the picture.
1print(" /\\_/\\")2print("( o.o )")3print(" > ^ <")
โA quote inside the text
A quote " starts and ends a piece of text. So what if we want to print a quote? We put a backslash before it: \". Then Python knows it isn't the end of the text, just a character to print.
Ola says OK ๐ฌ
Click Next and see how \" turns into a quote.
A message from Ola
Bajtek asks Ola to go for a bike ride with him ๐ฒ, and Ola answers OK!. Bajtek wants the program to print exactly what Ola said - with the quotes: Ola: "OK!".
A quote inside the text
A quote normally ends the text. \" tells Python: this isn't the end, print a quote.
print("Ola: \"OK!\"")
In the code
characters in the code: 12, on the screen: 10
Screen
Ola: "OK!"
Ola says
Two \" - one quote at the start of Ola's words, one at the end. Plain quotes stay on the screen.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Ola says: \"Hi!\"")
โA comic strip chat ๐ฌ
Bajtek and Kuba talk like in comic speech bubbles. Each one's words are in quotes - in the code every such quote is written as \".
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Bajtek: \"Hi, Kuba!\"")2print("Kuba: \"Hi! Shall we play football?\"")3print("Bajtek: \"Sure!\"")
โA new line: \n
\n is two characters in the code, but on the screen it's a move to a new line, as if someone pressed Enter. That way one print can print several lines.
A new line inside the text
\n is two characters in the code, but on the screen - a move to a new line.
print("Raz\nDwa")
In the code
characters in the code: 8, on the screen: 7
Screen
Raz Dwa
The simplest example: \n
One print and one text - but there's a \n inside. At that spot Python moves to a new line, so Ala and Ola end up one under the other.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Ala\nOla")
โOne print, two lines
The first program has two prints, the second only one - and the screen shows the same!
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("One")2print("Two")3print("One\nTwo")
โA shopping list in one print ๐
Three \n in one text - so the screen shows four lines: the title and three things to buy.
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("Shopping list:\n- bread\n- milk\n- apples")
โA trap: \n in a folder path
Folder paths in Windows have backslashes, e.g. C:\new. But \n is a new line! That's why in paths we write every backslash twice: C:\\new.
A broken and a fixed path
In the first line \n turned into a new line and the path fell apart. In the second \\ gives one backslash - and it all works!
Click the arrow - the computer runs the instructions in order, from top to bottom.
1print("C:\new folder")2print("C:\\new folder")
โSpecial characters in sep and end
Remember sep and end from lesson 8? They're text too, so they can have special characters. sep="\n" puts a new line between the values instead of a space - each value goes on its own line.
Names in a column
The same print twice: first with a normal space, then with sep="\n" - the names line up one under another.
1print("Ala", "Ola", "Kuba")2print("Ala", "Ola", "Kuba", sep="\n")
โWhat does the program print?
print("a\\b")
How do you print one backslash \ ?
How many lines does the program print?
print("A\nB\nC")
With backslashes you can draw roofs and rockets! Remember: every \ in the code is \\.
A house with a roof
Five print instructions - five lines on the screen. The roof has slashes on both sides.
Click the arrow - every print draws one more line of the picture.
1print(" /\\")2print(" / \\")3print(" /____\\")4print(" | |")5print(" |_[]_|")
โA rocket ๐
The tip and the wings are slashes. Flames at the bottom!
Click the arrow - every print draws one more line of the picture.
1print(" /^\\")2print(" | |")3print(" | O |")4print(" | |")5print(" /| |\\")6print(" *****")7print(" ***")
โRemember
- A backslash
\combines with the next character into one special character. \\- one backslash on the screen. The ordinary/is written as usual.\"- a quote inside the text.\n- a new line. Watch out for folder paths!
Now it's your turn to draw kittens and mountains! The tasks are waiting. ๐ฑ
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.