printing non-ASCII characters - please explain
Alex Martelli
aleax at aleax.it
Sun Jun 15 09:54:21 EDT 2003
Gerhard Häring wrote:
> 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.
str(), actually -- that's the reason the umlaut shows when you
just print N.
> 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-
Yes, and __str__ is much the same, which is where the problem comes in.
A list's __str__ calls repr(), NOT str(), on the items (because it's
felt that using an item's str() might be confusing if that item was a
string including commas, basically).
>> - 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.
Yes, this advice is indeed the only way to go.
Alex
More information about the Python-list
mailing list