943,800 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1879
  • Python RSS
Sep 13th, 2009
0

Python string reversed explanation

Expand Post »
I was googling for a way to reverse a string and I found this.

python Syntax (Toggle Plain Text)
  1. >>> 'asd'[::-1]
  2. >>> '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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009
Sep 13th, 2009
1

Re: Python string reversed explanation

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
Last edited by sneekula; Sep 13th, 2009 at 3:22 pm.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 13th, 2009
0

Re: Python string reversed explanation

wow ^^ even i didn't know that
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Friktion is offline Offline
15 posts
since Jun 2009
Sep 13th, 2009
0

Re: Python string reversed explanation

Click to Expand / Collapse  Quote originally posted by sneekula ...
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
I have used slice notation in the past. I wasn't aware that there was the 'step'. Thanks for your reply and link to the post.
Reputation Points: 10
Solved Threads: 0
Light Poster
nunos is offline Offline
46 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Checkbutton with Tkinter
Next Thread in Python Forum Timeline: Python Turtle module, and math module problem.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC