Printing and Printing in Python
Now we are going to do a bunch of exercises where you just type code in and make it run. I won’t be explaining much since it is just more of the same. The purpose is to build up your chops. See you in a few exercises, and do not skip! Do not paste!
1 formatter = "%r %r %r %r" 2 3 print formatter % (1, 2, 3, 4) 4 print formatter % ("one", "two", "three", "four") 5 print formatter % (True, False, False, True) 6 print formatter % (formatter, formatter, formatter, formatter) 7 print formatter % ( 8 "I had this thing.", 9 "That you could type up right.", 10 "But it didn't sing.", 11 "So I said goodnight." 12 )
1 2 3 4 'one' 'two' 'three' 'four' True False False True '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' 'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
1. Do your checks of your work, write down your mistakes, and try not to make them on the next exercise.
2. Notice that the last line of output uses both single- quotes and double- quotes for individual pieces. Why do you think that is?
Printing and Printing and Printing in Python
1 # Here's some new strange stuff, remember type it exactly. 2 3 days = "Mon Tue Wed Thu Fri Sat Sun" 4 months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" 5 6 print "Here are the days: ", days 7 print "Here are the months: ", months 8 9 print """ 10 There's something going on here. 11 With the three double- quotes. 12 We'll be able to type as much as we like. 13 Even 4 lines if we want, or 5, or 6. 14 """
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.
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.