Exercise 8: Printing, Printing

We will now see how to do a more complicated formatting of a string. This code looks complex, but if you do your comments above each line and break each thing down to its parts, you'll understand it.

formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, False, True))
print(formatter.format(formatter, formatter, formatter, formatter))
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear"

What You Should See

$ python3.6 ex8.py
1 2 3 4
one two three four
True False False True
{} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
Try your Own text here Maybe a poem Or a song about fear

In this exercise I'm using something called a "function" to turn the formatter variable into other strings. When you see me write formatter.format(...) I'm telling python to do the following:

  1. Take the formatter string defined on line 1.
  2. Call its format function, which is similar to telling it to do a command line command named format.
  3. Pass to format four arguments, which match up with the four {} in the formatter variable. This is like passing arguments to the command line command format.
  4. The result of calling format on formatter is a new string that has the {} replaced with the four variables. This is what print is now printing out.

That's a lot for the eighth exercise, so what I want you to do is consider this a brain teaser. It's alright if you don't really understand what's going on because the rest of the book will slowly make this clear. At this point, try to study this and see what's going on, then move on to the next exercise.

Study Drills

Repeat the Study Drill from Exercise 7.

Common Student Questions

Why do I have to put quotes around "one" but not around True or False?
Python recognizes True and False as keywords representing the concept of true and false. If you put quotes around them then they are turned into strings and won't work. You'll learn more about how these work in Exercise 27.
Can I use IDLE to run this?
No, you should learn to use the command line. It is essential to learning programming and is a good place to start if you want to learn about programming. IDLE will fail for you when you get further in the book.

Buy The Python 3 Edition!

When you buy Learn Python 3 The Hard Way, you'll receive the Python 3 Edition PDF, special access to a paid HTML version, and 12 hours of 1080p video, one video for each exercise. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.


Buy Digital Download From Zed

Try a a free sample of Learn Python the Hard Way right here, video lectures not included.

Other Buying Options

Buy on Amazon Buy from Barnes & Noble