| | |
Python string reversed explanation
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
I was googling for a way to reverse a string and I found this.
It works, but there was no explanation where I got if from. Can someone please be kind enough to explain it. Thanks.
python Syntax (Toggle Plain Text)
>>> 'asd'[::-1] >>> 'dsa'
It works, but there was no explanation where I got if from. Can someone please be kind enough to explain it. Thanks.
Last edited by nunos; Sep 13th, 2009 at 12:53 pm.
You have to read up on the slicing operator in the Python Manual.
Generally you can slice a sequence with [start: end: step]
By default start=0, end=len(seq), step=1
step=-1 goes from the end to the beginning of the sequence, hence your reverse.
For some nice slicing examples see post #4 at;
http://www.daniweb.com/forums/post10...tml#post104865
Generally you can slice a sequence with [start: end: step]
By default start=0, end=len(seq), step=1
step=-1 goes from the end to the beginning of the sequence, hence your reverse.
For some nice slicing examples see post #4 at;
http://www.daniweb.com/forums/post10...tml#post104865
Last edited by sneekula; Sep 13th, 2009 at 3:22 pm.
No one died when Clinton lied.
•
•
Join Date: Aug 2009
Posts: 35
Reputation:
Solved Threads: 0
•
•
•
•
You have to read up on the slicing operator in the Python Manual.
Generally you can slice a sequence with [start: end: step]
By default start=0, end=len(seq), step=1
step=-1 goes from the end to the beginning of the sequence, hence your reverse.
For some nice slicing examples see post #4 at;
http://www.daniweb.com/forums/post10...tml#post104865
![]() |
Similar Threads
- Confusing strings (Python)
- Converting string to python code (Python)
- extra string (Python)
- python string encoding (Python)
- Storing a Template String? (Python)
- hex string, reverse and move 4 LSB ??? (Python)
- Size of string object??? (C++)
- very 1st python tutorial 4 newbies (Python)
Other Threads in the Python Forum
- Previous Thread: Checkbutton with Tkinter
- Next Thread: Python Turtle module, and math module problem.
| Thread Tools | Search this Thread |
Tag cloud for python, reverse, slicenotation, string
abrupt access advanced advice apax array beginner binary book c# c++ calculator char character code console convert coordinates corners count curves cx-freeze data dictionary django drawing dynamic editing embed encryption enter event examples excel file filename format fstream ftp function gui iframe input int ip itunes java line linux list listbox lists loan loop memory mouse movingimageswithpygame newb opensource parse path program programming projects py2exe pygame pygtk python random read recursion recursive redirect regex reverse ruby rubyconf saving simple single smtp ssh string superscript table tennis threading tkinter tutorial ubuntu unicode urllib urllib2 user validation variable wikipedia wordgame write wxpython







