Python Unicode strangeness.

Andrew Kuchling akuchlin at mems-exchange.org
Thu Feb 8 10:42:06 EST 2001


"Syver Enstad" <syver at NOSPAMcyberwatcher.com> writes:
> '%s, %s' % ('æøasfd', u'a')
> this throws unicode error.
> Is this by design?

I believe so, though either MAL or /F are more likely to give a
definitive reply.  Because one of the arguments to % is a Unicode
string, it will coerce everything to Unicode.  The encoding used when
coercing a regular string into a Unicode string is 7-bit ASCII, so
only characters <128 are handled.  The first string in the tuple has
characters >128, so a UnicodeError is raised.  If you make it a
Unicode string, there's no problem since it doesn't need to be
coerced; if you make 'a' a regular string, no coercion is attempted.
In either case, the exception isn't raised. 

--amk





More information about the Python-list mailing list