livepython.net
CHAPTER 02The Print Function
2.2  ESCAPE SEQUENCES

Escape Sequences in Python: \n, \t and More

New Lines with \n

So far, every print() shows one line. But what if you want one print() to show several lines? You use a special code inside the text: \n.

PYTHONedit me
print("Line one\nLine two")
OUTPUT
Line one
Line two

\n means new line. It's like pressing Enter in the middle of your text. Everything after it goes to the next line.

What is an escape sequence?

A backslash \ inside a string is special. It tells Python: "the next character is not normal text, it's an instruction". The backslash and the character after it are called an escape sequence. They escape from their normal meaning.

\n is not printed. Python sees it, and starts a new line instead.

As many lines as you like

You can use \n again and again:

PYTHONedit me
print("Sunny\nRocky\nLucky")
OUTPUT
Sunny
Rocky
Lucky

One print(), three lines. This keeps short lists and messages in a single line of code.

Common beginner doubt: "Do I need spaces around \n?" No. A space before \n would end up at the end of the first line, and a space after it at the start of the next line. Usually you don't want either.

\n starts a new line, right in the middle of your text.

your turnType the output

Type what this prints

print("Hi\nThere")
Read the code line by line, then type the output exactly.
your turnFill the gap

Make it print two lines

print("Good?morning")

It should print: Good morning

Which piece makes this code work?

Tabs with \t

\t adds a tab, just like pressing the Tab key. Tabs are great for lining text up in neat columns:

PYTHONedit me
print("Name:\tSunny\nAge:\t25")
OUTPUT
Name:	Sunny
Age:	25

Sunny and 25 start at the same place, even though Name: and Age: have different lengths. How?

How a tab really works

A tab doesn't add a fixed number of spaces. It jumps to the next tab stop. On most screens, including LivePython, tab stops are every 8 characters: at position 8, 16, 24, and so on.

  • Name: is 5 characters, so \t jumps to position 8.
  • Age: is 4 characters, so \t also jumps to position 8.

That's why the second column lines up.

When a word is too long

If the text before a tab is 8 characters or more, the tab jumps to the next stop, 16. Then the columns don't line up any more. The fix: add an extra tab to the shorter lines.

PYTHONedit me
print("Name:\t\tSunny")
print("Location:\tDelhi")
OUTPUT
Name:		Sunny
Location:	Delhi

Location: is 9 characters, so one tab jumps to 16. Name: needs two tabs to reach 16 too.

\t jumps to the next tab stop, so your columns line up.

your turnPredict the output

What will this print?

print("A\tB")
Your turn! What will this code show on the screen?

Quotes Inside Text

What if your text has a quote mark in it, like It's sunny? Try it with single quotes:

PYTHONedit me
print('It's sunny')
OUTPUT
SyntaxError: unterminated string literal (detected at line 1)

Python sees the ' in It's and thinks the text ends there. Then s sunny') makes no sense to it, so it gives a SyntaxError.

There are two easy fixes.

Fix 1: use the other kind of quote

If the text has a single quote inside, wrap it in double quotes, and the other way round:

PYTHONedit me
print("It's sunny")
print('She said "Hi"')
OUTPUT
It's sunny
She said "Hi"

Fix 2: escape the quote

Put a backslash before the quote. \' and \" mean "this quote is part of the text":

PYTHONedit me
print('It\'s sunny')
print("She said \"Hi\"")
OUTPUT
It's sunny
She said "Hi"

Both fixes give the same result. Fix 1 is usually easier to read. Fix 2 helps when your text has both kinds of quotes.

EscapePrints
\''
\""

A quote inside text? Use the other kind of quote around it, or put a backslash before it.

your turnSpot the bug

Which line breaks?

One line stops this program. Tap it.
your turnFix the code

Make it print: She said "Hi"

It should print: She said "Hi"

print("She said "Hi"")
This code does not work yet. Fix it, then press Run.

Printing a Backslash

If a backslash starts every escape sequence, how do you print a real backslash? You escape it too! \\ prints one \:

PYTHONedit me
print("C:\\Users\\Sunny")
OUTPUT
C:\Users\Sunny

This is common when you print file paths on Windows, which use backslashes.

The \n trap

Watch what happens with only one backslash, when the next letter happens to be n:

PYTHONedit me
print("C:\new folder")
OUTPUT
C:
ew folder

Python saw \n and started a new line! The n of new disappeared into the escape sequence. Doubling the backslash, C:\\new folder, fixes it.

All the escape sequences you need

Escape sequenceWhat it does
\nstarts a new line
\tjumps to the next tab stop
\'prints a single quote
\"prints a double quote
\\prints one backslash

There are a few more, but these five cover almost everything you'll need.

Two backslashes print one. One backslash starts an escape sequence.

your turnPredict the output

What will this print?

print("C:\\Games")
Your turn! What will this code show on the screen?
your turnPick the right idea

Python shows: line 3, NameError: name 'prnt' is not defined. Where do you look first?

Think about it, then pick one.

Practice: Escape sequences

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 5.Python Program to Print Multiple Lines with One print()EasySolve
  2. 6.Python Program to Create a Name Card Using TabsEasySolve
  3. 7.Python Program to Print Nested Quotes with Escape CharactersEasySolve
  4. 14.Python Program to Print Escape Sequences as TextEasySolve
  5. 11.Python Program to Print a Digital ReceiptMediumSolve