livepython.net
CHAPTER 02The Print Function
2.4  TRIPLE QUOTES

Triple Quotes in Python: Multi-Line Strings

Write Text Over Many Lines

With \n you can print several lines, but long text full of \n is hard to read. Python has an easier way: triple quotes.

Put three quote marks at the start and three at the end. Then just press Enter wherever you want a new line:

PYTHONedit me
print("""Roses are red,
Violets are blue,
Python is fun,
And so are you!""")
OUTPUT
Roses are red,
Violets are blue,
Python is fun,
And so are you!

What you see in the code is what you get on the screen. No \n needed.

Three single or three double quotes

Both work the same, just like normal strings:

PYTHONedit me
print('''Line 1
Line 2''')
print("""Line 3
Line 4""")
OUTPUT
Line 1
Line 2
Line 3
Line 4

Start and end with the same kind: ''' with ''', or """ with """.

Everything is kept, even spaces

Triple quotes keep your text exactly as you typed it, including spaces at the start of a line:

PYTHONedit me
print("""Menu:
  - Tea
  - Coffee""")
OUTPUT
Menu:
  - Tea
  - Coffee

That's handy for lists and art. But be careful: if you add spaces by accident, they get printed too.

Common beginner doubt: "Can I still use \n and \t inside triple quotes?" Yes! Escape sequences work in every kind of string.

\n or triple quotes?

Both make several lines, so which one should you use?

  • For two or three short lines, \n is quick: print("Yes\nNo").
  • For longer text, like a poem, a menu or instructions, triple quotes are easier to read and to change later, because the code looks like the output.

There's no wrong choice. Pick the one that makes your code easiest to read.

Triple quotes print text exactly as you typed it, over as many lines as you like.

your turnPredict the output

What will this print?

print("""One Two Three""")
Your turn! What will this code show on the screen?

Quotes Inside Triple Quotes

Remember the trouble with It's in single quotes? Triple quotes make that easy. Inside them, you can use both kinds of quote freely:

PYTHONedit me
print("""It's called "Python", and it's fun.""")
OUTPUT
It's called "Python", and it's fun.

No backslashes needed. Python only ends the text when it sees three quotes in a row.

Why this helps

Text with conversations or quotes inside is much easier to write:

PYTHONedit me
print("""Rocky said, "Let's learn Python!"
Sunny said, "I'm in!" """)
OUTPUT
Rocky said, "Let's learn Python!"
Sunny said, "I'm in!"

With normal quotes, you'd need a backslash before almost every quote mark. With triple quotes, the code looks just like the output.

One small trap

If your text ends with a double quote, it sits right next to the closing """, and Python gets confused about where the text ends. Two easy fixes:

  • add a space before the closing quotes, as in the example above (the space is printed, but you won't notice it at the end of a line), or
  • use ''' around text that ends with ".

Inside triple quotes, both kinds of quote are just text.

your turnFix the code

Make it print: It's "great"

It should print: It's "great"

print('It's "great"')
This code does not work yet. Fix it, then press Run.

Draw Boxes and Banners with print()

Now that you can print several lines exactly as you type them, you can draw with text! Programmers call this text art. It's how old games and many command-line tools make their screens.

A simple box

PYTHONedit me
print("""+----------------+
|   LivePython   |
+----------------+""")
OUTPUT
+----------------+
|   LivePython   |
+----------------+

The lines must be the same length for the box to look right. Count the characters, or line them up in the editor.

A menu with tabs

Escape sequences work inside triple quotes, so you can use \t to line up prices:

PYTHONedit me
print("""=== Sunny's Cafe ===
1. Tea\t\t$1.50
2. Coffee\t$2.00
3. Cake\t\t$3.00""")
OUTPUT
=== Sunny's Cafe ===
1. Tea		$1.50
2. Coffee	$2.00
3. Cake		$3.00

2. Coffee is 9 characters, so one tab jumps it to position 16. The shorter lines need two tabs to get there too.

A banner with stars

PYTHONedit me
print("*" * 20)
print("   HAPPY BIRTHDAY")
print("*" * 20)
OUTPUT
********************
   HAPPY BIRTHDAY
********************

Here's a small trick: "*" * 20 repeats the star 20 times, so you don't have to count them. You'll learn more about repeating text in the Data Types chapter.

Your turn to design

Change the box to show your own name. Make a banner with stars, or a menu for your favourite food. Here are a few characters that draw nicely: - = + | * #.

This is also a great way to practise everything from this chapter: text, numbers, escape sequences, sep, end and triple quotes, all in one program.

With triple quotes, print() becomes a drawing tool.

your turnSpot the bug

Which line breaks?

One line stops this program. Tap it.
your turnPredict the output

What will this print?

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

Practice: Triple quotes

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

  1. 10.Python Program to Print Multiple Lines Without Escape SequencesEasySolve
  2. 12.Python Program to Print a Console DashboardHardSolve