Background:
Python has two built-in functions for turning an object into a string: str versus repr . str should be a friendly, human-readable string. repr should contain detailed information about the contents of the object (sometimes they return the same as for integers). By convention, if there is a Python expression that will be eval for another object that ==, repr will return such an expression, for example.
>>> print repr ('hi')
'hi' # notice the quotes here as opposed to ...
>>> print str ('hi')
hi
If returning an expression does not make sense for the object, repr should return a string surrounded by <and> characters, for example. <blah> .
To answer your original question:
%s ↔ str
%r ↔ repr
Besides:
You can control how you convert instances of your own classes to strings, __str__ and __repr__ .
class Foo: def __init__(self, foo): self.foo = foo def __eq__(self, other): """Implements ==.""" return self.foo == other.foo def __repr__(self):
allyourcode Mar 01 '10 at 8:48 2010-03-01 08:48
source share