Character Encodings and display of strings

JKPeck JKPeck at
Mon Nov 13 15:56:31 CET 2006

Thanks for the quick answer.  I thought repr was involved here, but
when I use repr explicitly I get a notation where the backslashes are
escaped.  I also though that with the encoding explictily declared in
the source, that repr would take that into account and use the
character form, but obviously it doesn't.

Fredrik Lundh wrote:
> "JKPeck" wrote:
> >I am trying to understand why, with nonwestern strings, I sometimes get
> > a hex display and sometimes get the string printed as characters.
> >
> > With my Python locale set to Japanese and with or without a # coding of
> > cp932 (this is Windows) at the top of the file, I read a list of
> > Japanese strings into a list, say, catlis.
> >
> > With this code
> > for item in catlis:
> >       print item
> > print catlis
> > print " ".join(catlis)
> >
> > the first print (print item) displays Japanese text as characters..
> > The second print (print catlis) displays a list with the double byte
> > characters in hex notation.
> > The third print (print " ".join(catlis)) prints a combined string of
> > Japanese characters properly.
> >
> > According to the print documentation,
> > "If an object is not a string, it is first converted to a string using
> > the rules for string conversions"
> >
> > but the result is different with a list of strings.
> a list is not a string, so it's converted to one using the standard list representation
> rules -- which is to do repr() on all the items, and add brackets and commas as
> necessary.
> for some more tips on printing, see:
> </F>

More information about the Python-list mailing list