88

I cannot understand this. I have seen this in people's code. But cannot figure out what it does. This is in Python.

str(int(a[::-1]))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
sofa_maniac
  • 1,659
  • 2
  • 12
  • 21
  • 2
    What datatype is `a`? – xrisk Jul 26 '15 at 04:35
  • possible duplicate of [What does 'result\[::-1\]' mean?](http://stackoverflow.com/questions/13365424/what-does-result-1-mean), or better yet: [Explain Python's slice notation](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) – Jeff Mercado Jul 26 '15 at 04:37
  • This is some silly code, because the `int()` call is effectively a no-op (it doesn't do anything), because the result is converted to a string. – Burhan Khalid Jul 26 '15 at 06:04
  • 2
    @BurhanKhalid, you cannot consider that a no-op. If the string is actually not convertible to an integer you will get an exception (ValueError if I recall correctly) before re-converting to string. If you remove that cast to int you lose that (quick and easy) sanity check and the code will just blindly reverse any string no matter if it's the representation of an integer or not. – vinnie Oct 30 '19 at 15:08

2 Answers2

180

Assuming a is a string. The Slice notation in python has the syntax -

list[<start>:<stop>:<step>]

So, when you do a[::-1], it starts from the end towards the first taking each element. So it reverses a. This is applicable for lists/tuples as well.

Example -

>>> a = '1234'
>>> a[::-1]
'4321'

Then you convert it to int and then back to string (Though not sure why you do that) , that just gives you back the string.

Bat
  • 771
  • 11
  • 29
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 2
    If I were to fill out all three indexes explicitly, what would they be? – xrisk Jul 26 '15 at 04:57
  • 7
    To get the same result , you cannot fill out all three, you can put as `len(a)-1` , but there is no `` index you can put to get the first element. Example - `a[len(a)-1::-1]` . This is because `` index is exclusive, that is the element at that index is not taken in, also `-1` means the last element , so there is no index that can be used to include the first element , other than leaving it blank – Anand S Kumar Jul 26 '15 at 04:58
  • 2
    The conversion from int to string might be a hacky way to remove leading zeros in the reversed string – Tomas Wilson Mar 22 '21 at 05:19
  • @xrisk `a[-1:-len(a)-1:-1])` , if you really want to check the values for slice method of an array you can use it, hope this will help – Amit Dwivedi Jan 16 '23 at 06:27
49

The notation that is used in

a[::-1]

means that for a given string/list/tuple, you can slice the said object using the format

<object_name>[<start_index>, <stop_index>, <step>]

This means that the object is going to slice every "step" index from the given start index, till the stop index (excluding the stop index) and return it to you.

In case the start index or stop index is missing, it takes up the default value as the start index and stop index of the given string/list/tuple. If the step is left blank, then it takes the default value of 1 i.e it goes through each index.

So,

a = '1234'
print a[::2]

would print

13

Now the indexing here and also the step count, support negative numbers. So, if you give a -1 index, it translates to len(a)-1 index. And if you give -x as the step count, then it would step every x'th value from the start index, till the stop index in the reverse direction. For example

a = '1234'
print a[3:0:-1]

This would return

432

Note, that it doesn't return 4321 because, the stop index is not included.

Now in your case,

str(int(a[::-1]))

would just reverse a given integer, that is stored in a string, and then convert it back to a string

i.e "1234" -> "4321" -> 4321 -> "4321"

If what you are trying to do is just reverse the given string, then simply a[::-1] would work .

Abhilash Panigrahi
  • 1,455
  • 1
  • 13
  • 31