views:

1005

answers:

8

I was looking for a way to print a string backwards, and after a quick search on google, I found this method:

Suppose 'a' is a string variable. This will return the 'a' string backwards:

a[::-1]

Can anyone explain how that works?

+2  A: 

It's called Slice Notation in Python and you can read a bit more of how it works here:

http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation

Gary Willoughby
+1  A: 

http://www.python.org/doc/2.3.5/whatsnew/section-slices.html

It's the extended slice notation:

sequence[start:end:step]

In this case, step -1 means backwards, and omitting the start and end means you want the whole string.

Steve Jessop
+5  A: 

The "-1" part represents the "step" part of the slicing—in this case, it goes through the string 1 character at a time, but backwards (a negative step means start from the end of the string). If you specify the step to be 2, for instance, you would get every other character of the string, starting with the first one. If you specify a step of -2, then you'd get every other character of the string, starting with the last character and working backwards.

So, in a nutshell, if a = '12345':

  • a[::2] becomes 135
  • a[::-1] becomes 54321
  • a[::-2] becomes 531
htw
+1  A: 

It's using extended slicing - a string is a sequence in Python, and shares some methods with other sequences (namely lists and tuples). There are three parts to slicing - start, stop and step. All of them have default values - start defaults to 0, stop defaults to len(sequence), and step defaults to 1. By specifying [::-1] you're saying "all the elements in sequence a, starting from the beginning, to the end going backward one at a time.

This feature was introduced in Python 2.3.5, and you can read more in the What's New docs.

Adam
+20  A: 

Sure, the [::] is the extended slice operator. It allows you to take substrings. Basically, it works by specifying which elements you want as [begin:end:step], and it works for all sequences. Two neat things about it:

  • You can omit one or more of the elements and it does "the right thing"
  • Negative numbers for begin, end, and step have meaning

For begin and end, if you give a negative number, it means to count from the end of the sequence. For instance, if I have a list::

l = [1,2,3]

Then l[-1] is 3, l[-2] is 2, and l[-3] is 1.

For the step argument, a negative number means to work backwards through the sequence. So for a list::

l = [1,2,3,4,5,6,7,8,9,10]

You could write l[::-1] which basically means to use a step size of -1 while reading through the list. Python will "do the right thing" when filling in the start and stop so it iterates through the list backwards and gives you [10,9,8,7,6,5,4,3,2,1].

I've given the examples with lists, but strings are just another sequence and work the same way. So a[::-1] means to build a string by joining the characters you get by walking backwards through the string.

Rick Copeland
+1  A: 

[::-1] gives a slice of the string a. the full syntax is a[begin:end:step] which gives a[begin], a[begin+step], ... a[end-1]. WHen step is negative, you start at end and move to begin.

Finally, begin defaults to the beginning of the sequence, end to the end, and step to -1.

Andrew Jaffe
+1  A: 

a string is essentially a sequence of characters and so the slicing operation works on it. What you are doing is in fact:

-> get an slice of 'a' from start to end in steps of 1 backward.

fengshaun
+2  A: 

I think the following makes a bit more sense for print strings in reverse, but maybe that's just me:

for char in reversed( myString ):  
  print( char, end = "" )
Stefan Kendall
what's that 'end = ""' for?
David McDavidson
It's the python 3 way of not newlining at the end of each print.In python 2.x, you could just writeprint( char, )
Stefan Kendall
Of course, this prints out the string; it does not create a new string that is the old string reversed (''.join(reversed(myString)) will do that, however).
Rick Copeland
Indeed. However, the OP only asked for "printing." Hence a "printing" solution.
Stefan Kendall
you could allways do: ''.join(reversed(myString0)) to get the string.
Daren Thomas
@iftrue: you're right, sorry about the -1. I will remove it if you edit the answer.
Rick Copeland
(the vote is too old to be changed unless the post is edited)
Rick Copeland