LPTHW Exercise 6: Strings and Text

Learn Python the Hard Way by Zed Shaw .: I am not entirely clear about the difference between %r vs %s in string formatting. I do understand they call different functions (repr and str) and produce different output:

>>> vessel = 'Hesperus'
>>> print "The Wreck of the %r" % vessel
The Wreck of the 'Hesperus'
>>> print "The Wreck of the %s" % vessel
The Wreck of the Hesperus
>>>

As the book states, the first example is "raw output" and the second is the content more cleanly formatted for printing. Why using repr would be desirable under certain circumstances will probably become more clear later on.

More • pythonprogramming