Exercise 8: Printing, Printing question

Check out this problem:

formatter = “%r %r %r %r”

print formatter % (formatter, formatter, formatter, formatter)

Answer: ‘%r %r %r %r’ ‘%r %r %r %r’ ‘%r %r %r %r’ ‘%r %r %r %r’

Shouldn’t the answer be %r %r %r %r? Why does it (%r %r %r %r) have to be stated FOUR times?

Well because formatter = "%r %r %r %r" not "%r"
So you print the variable formatter 4 times, not 4 strings “formatter” “formatter” “formatter” “formatter”.
Wondering why you do the LPTHW book and not LP3THW.
The same exercise in LP3THW is formatter = "{} {} {} {}"

So, break it down as to what happens:

  1. %r puts the contents of the first (formatter, into the string. That’s one %r %r % %r
  2. %r does it again, with ,formatter,
  3. %r does it again, with the next formatter.
  4. %r does it one more time with the last, formatter)

So, that puts the formatter inside the formatter 4 times.