str() should convert ANY object to a string without EXCEPTIONS !
Terry Reedy
tjreedy at udel.edu
Sun Sep 28 03:55:46 EDT 2008
est wrote:
>>From python manual
>
> str( [object])
>
> Return a string containing a nicely printable representation of an
> object. For strings, this returns the string itself. The difference
> with repr(object) is that str(object) does not always attempt to
> return a string that is acceptable to eval(); its goal is to return a
> printable string. If no argument is given, returns the empty string,
> ''.
>
>
> now we try this under windows:
>
>>>> str(u'\ue863')
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\ue863' in
> position 0
> : ordinal not in range(128)
In 3.0 this is fixed:
>>> str('\ue863') # u prefix is gone
'\ue863'
>>> str(b'123') # b prefix is added
"b'123'"
Problems like this at least partly motivated the change to unicode
instead of bytes as the string type.
tjr
More information about the Python-list
mailing list