"%s" % (obj,) "%s" % str(obj)
Now, what about this:
"%s %s" % (a, b) "%s %s" % (str(a), str(b))
It turns out that they aren’t! Given this code:
class Surprise(object): def __str__(self): return "[str]" def __unicode__(self): return u"[unicode]" surprise = Surprise() print "%s %s" % ("foo", surprise) print "%s %s" % (u"foo", surprise)
In short, as soon as a Unicode item has been interpolated into a bytestring (which makes the result a Unicode string), further items are automatically use Unicode conversion, given by __unicode__(), if available. This came up recently on the docutils mailing list, where a test failure was triggered because Python 2.6′s IOError has a buggy __unicode__() implementation.