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).
Comments
Comment from teneisha - September 30, 2006 at 3:56 am
just so you know….
http://37signals.com/svn/archives2/reflections_are_the_new_drop_shadows.php
Comment from adam - December 12, 2010 at 4:39 pm
Thanks best piece of code I’ve read all week