views:

41

answers:

2

I am using Python 2.6.5. My code requires the use of the "more than or equal to" sign. Here it goes:

>>> s = u'\u2265'
>>> print s
>>> ≥
>>> print "{0}".format(s)
Traceback (most recent call last):
     File "", line 1, in  
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2265'
  in position 0: ordinal not in range(128)`  

Why do I get this error? Is there a right way to do this? I need to use the .format() function.

+6  A: 

Just make the second string also a unicode string

>>> s = u'\u2265'
>>> print s
≥
>>> print "{0}".format(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2265' in position 0: ordinal not in range(128)
>>> print u"{0}".format(s)
≥
>>> 
Fabian
Thanks! It works now :)
Kit
If it works you should accept the answer.
daniels
@Kit: If you want all literals to be Unicode (like in Python 3), put `from __future__ import unicode_literals` at the beginning of your source files.
Philipp
@daniels: It said "You can accept an answer in 4 minutes". I probably waited 2 minutes too long. :)
Kit
A: 

unicodes need unicode format strings.

>>> print u'{0}'.format(s)
≥
Ignacio Vazquez-Abrams