invert the order of a string

Dave Hansen iddw at hotmail.com
Mon Feb 13 19:14:44 EST 2006


On Mon, 13 Feb 2006 19:03:32 +0000 in comp.lang.python, rtilley
<rtilley at vt.edu> wrote:

>Dave Hansen wrote:
>> How about 
>> 
>> s = "some random string"
>> print s
>> s = s[::-1]
>> print s
>
>That looks like Perl, but it works. Makes me wonder with the string 
>module doesn't have a reverse or invert function?

It's just simple slicing.  Well, maybe not so simple, or at least not
so common, but with a syntax similar to the range function.  Consider
the following (string chosen to make it obvious what's going on):

s = "0123456789"
s[::]
s[3::]
s[:3:]
s[::3]
s[::-2]
s[-2::-2]

Regards,
                                        -=Dave

-- 
Change is inevitable, progress is not.



More information about the Python-list mailing list