printing non-ASCII characters - please explain

Gerhard Häring gh at ghaering.de
Sun Jun 15 14:02:32 CEST 2003


Helmut Jarausch wrote:
> Hi,
> 
> please explain the following difference to me (with cvs-python)
> 
> N='H\xf6fig'
> 
> print N
> 
> this prints Höfig     which contains the Germain umlaut 'ö' = oe = \xf6
> 
> but
> 
> print [N]
> prints
> ['H\xf6fig']
> 
> why 

Because print invokes repr() on the object to print.

repr for lists is implemented in C, but if it were written in Python, it 
would probably look like:

#v+
class list:
     [...]
     def __repr__(self):
         return "[%s]" % ", ".join([repr(x) for x in self.items])
#v-

> - and how can I change this?

Use an explicit function to format the list how you want it to be. If 
you're not aware of the difference between str() and repr(), I'd 
recommend you look them up in the Python documentation.

-- Gerhard





More information about the Python-list mailing list