Basics

Module 2 · Basics · Lesson 5 of 9

Escape & newline characters

How do you tell print to break onto a new line, when you can't press Enter inside a string? You use a special code. A backslash \ inside a string is an escape character: it tells Python that the next character means something special.

\n means “new line”
print("Line 1\nLine 2")
output
Line 1
Line 2

The most common one is \n — a newline. It isn't printed as a backslash and an “n”; instead, print breaks the text onto the next line right where it sits. Its cousin \t inserts a tab.

Because the backslash is special, to print an actual backslash you write two of them: \\. Try moving the \n and \t around below and see what happens:

Run it yourselfruns in your browser · Python
main.py