Video Coming Soon...
9: Multi-line Strings
By now you should realize the pattern for this book is to use more than one exercise to teach you something new. I start with code that you might not understand, then more exercises explain the concept. If you don't understand something now, you will later as you complete more exercises. Write down what you don't understand, and keep going.
# Here's some new strange stuff, remember to type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
print("Here are the days: ", days)
print("Here are the months: ", months)
print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")
What You Should See
Here are the days: Mon Tue Wed Thu Fri Sat Sun
Here are the months: Jan
Feb
Mar
Apr
May
Jun
Jul
Aug
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
Study Drills
Repeat the Study Drill from Exercise 7.
Common Student Questions
Why do I get an error when I put spaces between the three double-quotes? You have to type them like
"""
and not" " "
, meaning with no spaces between each one.What if I wanted to start the months on a new line? You simply start the string with
\n
like this:"\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
.Is it bad that my errors are always spelling mistakes? Most programming errors in the beginning (and even later) are simple spelling mistakes, typos, or getting simple things out of order.
Register for Learn Python the Hard Way, 5th Edition (2023-2024)
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.