Which format?

12 Jun, 2014

Koki Y writes:

I have a question. You use % for format instead .format(). In the python tutorial, it is written that % format is an older version of format. What's the point of using % format?

The book uses the % operator (also known as the interpolation operator) basically through omission more than anything else. The first electronic version was written for Python2 (back in 2006/2007), then updated to be compatible with both 2 and 3. While it was almost totally rewritten, to be published as "Python for Kids", some of the existing text was repurposed -- however, the string formatting section wasn't specifically updated for Python 3. I suspect that's somehing we should probably fix when/if it's reprinted again.

By all means, use the format function as detailed in the Python documentation.