invert the order of a string

Carl Cerecke cdc at maxnet.co.nz
Mon Feb 13 21:35:25 EST 2006


Eric McGraw wrote:
>>Well, it turns out to be the best way to invert a string, IMO. The
>>reversed() feature returns a reversed object... not a reversed string.
>>In short, I have to fool with it again _after_ it has been inverted. The
>>slicing takes care of the job right away and gives me what I want... no
>>Computer Sciencey <reversed object at 0xb6f6152c>> to deal with :)
> 
> 
> A <reversed object> can be turned back into a string:
>  >>> st = '0123456789'
>  >>> reversed(st)
>  <reversed object at 0x00A8CC50>
>  >>> ''.join( reversed(st) )
>  '9876543210'

But that's slower than new_s = s[::-1]

Cheers,
Carl.



More information about the Python-list mailing list