Let's talk about multiline strings in Python.
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.
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.
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.
If you need to represent a block of text in your Python code that spans over multiple lines, you can use a multiline string.
Python Jumpstart is designed to help new Python programmers get up to speed quickly. Get hands-on practice with 50 bite-sized modules that build your Python skills step by step.
Sign up for my 5 day email course and learn essential concepts that introductory courses often overlook!
Sign in to your Python Morsels account to track your progress.
Don't have an account yet? Sign up here.
Sign up for my free 5 day email course and learn essential concepts that introductory courses often overlook: iterables, callables, pointers, duck typing, and namespaces. Learn to avoid beginner pitfalls, in less than a week!
Ready to level up? Sign up now to begin your Python journey the right way!