I was googling for a way to reverse a string and I found this.
>>> '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.
I was googling for a way to reverse a string and I found this.
>>> '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.
Jump to PostYou 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 …
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/post104865.html#post104865
wow ^^ even i didn't know that
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/post104865.html#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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.