invert or reverse a string... warning this is a rant

Brad rtilley at vt.edu
Thu Oct 19 18:38:55 CEST 2006


John Salerno wrote:
> rick wrote:
>> Why can't Python have a reverse() function/method like Ruby?
> 
> I'm not steeped enough in daily programming to argue that it isn't 
> necessary, but my question is why do you need to reverse strings? Is it 
> something that happens often enough to warrant a method for it?

I'm home for lunch so my email addy is different.

No, it doesn't happen very often, but when I need to reverse something 
(usually a list or a string). I can never remember right of the top of 
my head how to do so in Python. I always have to Google for an answer or 
refer back to old code.

IMO, I should be able to intuitively know how to do this. Python is so 
user-friendly most every place else... why can it not be so here?

I wrote this so I'll never have to remember this again:

def invert(invertable_object):
     try:
         print invertable_object[::-1]
         return invertable_object[::-1]
     except:
         print 'Object not invertable'
         return 1

invert([1,2,3,4])
invert('string')
invert({1:2, 3:4})







More information about the Python-list mailing list