"%s" vs unicode

Terry Reedy tjreedy at udel.edu
Tue Jan 7 17:46:53 EST 2003


"Robin Becker" <robin at jessikat.fsnet.co.uk> wrote in message
news:1GtMGLAYnyG+Ew34 at jessikat.demon.co.uk...
> A user reports that an old style error message is being smashed as
the
> message was being turned into unicode.
>
> the situation was of the form
>
> raise "xxxx %s' % var
>
> where var was a lump of unicode. We can easily correct the
sistuation.
>
> We didn't expect unicode, but what's surprising is that "%s" %
u'A' -->
> u'A'.

u'1234' works just as well, with a corresponding output.

>Doesn't %s mean take the str()?  I looked in vain in the docs
> for the definition of exactly what "%s" % x is supposed to mean,

 The doc (LibRef 2.2.6.2 String Formatting Operations) is slightly
contradictory.  In the table for % it does say

s String (converts any python object using str()).

but the first paragraph ends with "If format is a Unicode object, or
if any of the objects being converted using the %s conversion are
Unicode objects, the result will be a Unicode object as well. "  I
reported this as SF bug 664044.

Your user's problem appear to arise from the unicode result.

>>> raise u'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: exceptions must be strings, classes, or instances, not
unicode

Terry J. Reedy






More information about the Python-list mailing list