Exercise 9: Printing, Printing, Printing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Here's some new strange stuff, remember 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

$ python ex9.py
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

  1. Do your checks of your work, write down your mistakes, try not to make them on the next exercise.

Common Student Questions

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"
Why do the \n newlines not work when I use %r?
That's how %r formatting works; it prints it the way you wrote it (or close to it). It's the "raw" format for debugging.
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.
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.
Loading...