Multiline strings (original) (raw)

Sign in to your Python Morsels account to save your screencast settings.

Don't have an account yet? Sign up here.

Let's talk about multiline strings in Python.

A string with line breaks in it

Here we have a Python program called stopwatch.py that acts like a timer:

`from itertools import count from time import sleep import sys

arguments = sys.argv[1:]

usage = "Welcome to stopwatch!\nThis script counts slowly upward,\none second per tick.\n\nNo command-line arguments are accepted." if arguments: sys.exit(usage)

for n in count(): print(f"{n} sec") sleep(1) `

It counts upward one second at a time, until we manually exit it by hitting Ctrl + C:

$ python3 stopwatch.py 0 sec 1 sec 2 sec 3 sec 4 sec 5 sec ^CTraceback (most recent call last): File "/home/trey/stopwatch.py", line 13, in <module> sleep(1) KeyboardInterrupt

If we run this program with any command-line arguments at all, it prints out a usage statement instead of counting:

`~ $ python3 stopwatch.py --help Welcome to stopwatch! This script counts slowly upward, one second per tick.

No command-line arguments are accepted. `

This usage statement is represented by multiple lines of text in a single string.

usage = "Welcome to stopwatch!\nThis script counts slowly upward,\none second per tick.\n\nNo command-line arguments are accepted."

This string has \n characters in it, which are newline characters.

Using string concatenation to build up long strings

Currently our string is pretty long.

usage = "Welcome to stopwatch!\nThis script counts slowly upward,\none second per tick.\n\nNo command-line arguments are accepted."

It'd be nice to break it up over multiple lines.

We could make this code a little bit more readable by breaking this string up into substrings and then concatenating each of these substrings together:

usage = ( "Welcome to stopwatch!\n" + "This script counts slowly upward,\n" + "one second per tick.\n" + "\n" + "This script does not accept command-line arguments." )

Each of these lines in our code represents one line of text (each line of text ends in a \n character) and we're using plus (+) to concatenate these lines together.

Now, notice these parentheses, those are necessary to tell Python that these lines are all a continuation of just one line of code. That's called an implicit line continuation.

Using implicit line continuation and string concatenation does work. But there's a better way to represent strings that span over multiple lines of text in Python.

Multiline strings

This is a multiline string:

`usage = """Welcome to stopwatch! This script counts slowly upward, one second per tick.

This script does not accept command-line arguments.""" `

These triple quotes ("""...""") tell Python that we're making a string that might span over multiple lines of code. So Python knows that it should treat all of the newline characters within this string as part of our string, until it hits the end triple quote.

Multiline strings can use triple double quotes ("""..."""), or triple single quotes ('''...'''). Either style is accepted by Python.

Triple quotes create multiline strings in Python

If you need to represent a block of text in your Python code that spans over multiple lines, you can use a multiline string.