How to Reverse String in Python using Extended Slice Syntax?
Reverse string in Python is very much simple. Simple code as ‘CSEStack'[::-1] reverse the string.
print 'CSEStack'[::-1] Output : kcatSESC
This syntax of string manipulation is called as Extended Slice Syntax. It is defined as [begin:end:step]. This feature has added in all the versions after Python 2.3.
- begin: Begin defines the starting index of string to clip. If you don’t mention begin index value, it takes default as index 0.
- end: End defines the last index of string to be sliced. If you don’t mention end index, it takes defaults as the last index of string
- step: It defines steps to be jumped from one char to next char. Default step value is 1.
Code to reverse String in python.
strName= 'CSE' print strName[::-1] Output: ESC
Extended Slice syntax also works for the List in python.