[Tutor] A unicode print question

Joel Goldstick joel.goldstick at gmail.com
Fri Jul 30 21:01:12 CEST 2010

I was reading this: http://diveintopython.org/xml_processing/unicode.html

and tried the exercise:

>>> s = u'La Pe\xf1a'

>>> print s

Traceback (innermost last):
  File "<interactive input>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> print s.encode('latin-1')

La Peña

But oddly enough, when I typed it into my python shell I did NOT get the
UnicodeError, and I wonder why not:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'La Pe\xf1a'
>>> print s
La Peña
>>> import sys
>>> sys.getdefaultencoding()

Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100730/a32c403f/attachment.html>

More information about the Tutor mailing list