AlgoBazaAlgoBaza
Python for kids ยท Module 1

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 codeOn the screen
\\\ - one backslash
\"" - a quote inside the text
\na 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.

Bajtek
Will you build a house?๐Ÿง’
๐Ÿƒ๐Ÿƒ๐ŸŽฏ/\
1

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

Step 1 / 7

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("\\")

\\โ†“\one backslash \

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.

Code
1print("\\")
Screen
โ–
1/2 ยท line 1 runs next

Character by character: /\\

The ordinary / goes to the screen unchanged, and the pair \\ turns into one backslash.

print("/\\")

/โ†“/
\\โ†“\one backslash \

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.

Code
1print("/\\")
2print("/\\/\\")
Screen
โ–
1/3 ยท line 1 runs next

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.

Code
1print(" /\\_/\\")
2print("( o.o )")
3print(" > ^ <")
Screen
โ–
1/4 ยท line 1 runs next

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.

Bajtek
OK!๐Ÿ‘ง
๐ŸŽฏthe screen should show: Ola: "OK!"
1

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!".

Step 1 / 7

A quote inside the text

A quote normally ends the text. \" tells Python: this isn't the end, print a quote.

print("Ola: \"OK!\"")

Oโ†“O
lโ†“l
aโ†“a
:โ†“:
โ†“space
\"โ†“"a quote inside the text
Oโ†“O
Kโ†“K
!โ†“!
\"โ†“"a quote inside the text

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.

Code
1print("Ola says: \"Hi!\"")
Screen
โ–
1/2 ยท line 1 runs next

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.

Code
1print("Bajtek: \"Hi, Kuba!\"")
2print("Kuba: \"Hi! Shall we play football?\"")
3print("Bajtek: \"Sure!\"")
Screen
โ–
1/4 ยท line 1 runs next

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")

Rโ†“R
aโ†“a
zโ†“z
\nโ†“โ†ตa new line
Dโ†“D
wโ†“w
aโ†“a

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.

Code
1print("Ala\nOla")
Screen
โ–
1/2 ยท line 1 runs next

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.

Code
1print("One")
2print("Two")
3print("One\nTwo")
Screen
โ–
1/4 ยท line 1 runs next

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.

Code
1print("Shopping list:\n- bread\n- milk\n- apples")
Screen
โ–
1/2 ยท line 1 runs next

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.

Code
1print("C:\new folder")
2print("C:\\new folder")
Screen
โ–
1/3 ยท line 1 runs next

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.

Code
1print("Ala", "Ola", "Kuba")
2print("Ala", "Ola", "Kuba", sep="\n")
Screen
โ–
1/3 ยท line 1 runs next
?

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.

Code
1print(" /\\")
2print(" / \\")
3print(" /____\\")
4print(" | |")
5print(" |_[]_|")
Screen
โ–
1/6 ยท line 1 runs next

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.

Code
1print(" /^\\")
2print(" | |")
3print(" | O |")
4print(" | |")
5print(" /| |\\")
6print(" *****")
7print(" ***")
Screen
โ–
1/8 ยท line 1 runs next

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.