Thursday, November 10, 2016


How to reverse a string in python?


In both python 2.x and 3.x you can use the extended slicing technique to reverse a string.

Note:- The first two bounds of S[::-1] default to 0 and the length of the sequence and a stride of −1 indicates that the slice should go from right to left instead of the usual left to right.


Also note that Strings are immutable sequences. Hence S[::-1] doesn't change S in place, instead it creates a new object as can be seen in both python 2.x and 3.x with the 'is' operator which tests object identity.





No comments:

Post a Comment