Python reverse string method

Saw this easy way to reverse a string in python today, over at Answer My Searches

Here’s how to do it:

exampleString = 'Hey there fancy pants'
reversedString = exampleString[::-1]
print reversedString
>> stnap ycnaf ereht yeH

This works because a String is a sequence type in python and you can apply all the ‘slicing’ syntax you want to it. So in this case [::-1] is saying, taking the string sequence start from the beginning ‘:’ go to the end ‘:’ and step through it in reverse (-1).

Posted: September 25th, 2006
Categories: Code, python
Tags:
Comments: 2 Comments.
Comments
Comment from teneisha - September 30, 2006 at 3:56 am
Comment from adam - December 12, 2010 at 4:39 pm

Thanks best piece of code I’ve read all week