invert the order of a string

Eric McGraw thelinuxmaster at gmail.com
Mon Feb 13 21:31:44 EST 2006


> 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'

>
> I'm sure the reversed feature is much more generic though for dealing
> with other types.




More information about the Python-list mailing list