Python string reversed explanation

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

Python string reversed explanation

 
0
  #1
Sep 13th, 2009
I was googling for a way to reverse a string and I found this.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Python string reversed explanation

 
1
  #2
Sep 13th, 2009
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 15
Reputation: Friktion is an unknown quantity at this point 
Solved Threads: 4
Friktion Friktion is offline Offline
Newbie Poster

Re: Python string reversed explanation

 
0
  #3
Sep 13th, 2009
wow ^^ even i didn't know that
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: nunos is an unknown quantity at this point 
Solved Threads: 0
nunos nunos is offline Offline
Light Poster

Re: Python string reversed explanation

 
0
  #4
Sep 13th, 2009
Originally Posted by sneekula View Post
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.
Reply With Quote Quick reply to this message  
Reply

Tags
python, reverse, slicenotation, string

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for python, reverse, slicenotation, string
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC