108

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]))
5
  • 2
    What datatype is a? Commented Jul 26, 2015 at 4:35
  • possible duplicate of What does 'result[::-1]' mean?, or better yet: Explain Python's slice notation Commented Jul 26, 2015 at 4: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. Commented Jul 26, 2015 at 6: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. Commented Oct 30, 2019 at 15:08
  • [::-1] is the same as np.flip but without the dependency on numpy (and also without its readability:) indices_sorted_descending = np.flip(indices_sorted_ascending) Commented Jun 11, 2025 at 8:00

2 Answers 2

206

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.

Sign up to request clarification or add additional context in comments.

If I were to fill out all three indexes explicitly, what would they be?
To get the same result , you cannot fill out all three, you can put <start> as len(a)-1 , but there is no <stop> index you can put to get the first element. Example - a[len(a)-1::-1] . This is because <stop> 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
The conversion from int to string might be a hacky way to remove leading zeros in the reversed string
@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
56

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.