← All lessons

Escaping: \n, \t, \"

Sign in · Sign up

Lesson 4.8

A backslash \ adds a special character to a string:

Form What it gives
\n a line break
\t a tab
\" a quote inside a double-quoted string
print("Menu:\n1. Play\n2. Quit")
print("Name:\tMia")
print("He said: \"Hello\"")

Special characters are written inside the string and only as lowercase letters: \N is not a line break. Without \ a quote would close the string too early.

The heading title and the menu items item_1 and item_2 are set by the check. Print the menu: the heading, then 1. <item_1> on a new line, then 2. <item_2> on the next line. Build all of it with \n in a single string.

Hint

A line break inside text is \n, written inside the string.

Build the whole block as one string: the heading, \n, the first item, \n, the second. Inside a string the quotes are escaped with a backslash.