3

What happens when we give string[::-1] in python such that the string gets reversed we know that by default the places are acquired by the 0 index and the last index+1 of the string or the -last index and -1 then how on writing above it starts counting from last index whether neither -1 is present at the first place nor the last index so that it starts decreasing 1 from where I can get depth knowledge of working of slicing in python and what does it return like stuff

3
  • '[::-1]' generates a slice object, slice(None, None, -1). This is given to the __getitem__ method of the string object. That method, written in C, takes care to implementation details. Each None is interpreted to mean the relevant end point. I don't think we need to know exactly how it does that, just so long as our intuition matches that actual behavior. Commented Aug 25, 2019 at 3:46
  • In the slice(None,None,-1) we can replace the first None with len(str), but there's nothing we can use inplace the second None. 0 stops too early. -1 means the other end. So we have to trust that the developers have done a good job of handling these 'end point' cases. Commented Aug 25, 2019 at 3:50
  • How to Reverse a String in Python & How to Reverse a List in Python will teach you more that you'll really ever want to know. Commented Aug 25, 2019 at 6:10

2 Answers 2

3

From the python documentation on slicing sequences like [i:j:k]

The slice of s from i to j with step k is defined as the sequence of items with index x = i + n*k such that 0 <= n < (j-i)/k. In other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping when j is reached (but never including j). When k is positive, i and j are reduced to len(s) if they are greater. When k is negative, i and j are reduced to len(s) - 1 if they are greater. If i or j are omitted or None, they become “end” values (which end depends on the sign of k). Note, k cannot be zero. If k is None, it is treated like 1.

https://docs.python.org/3/library/stdtypes.html#common-sequence-operations

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

3 Comments

Here what is meant that when k is positive i and j are reduced to len(s) because when k is positive i is increased
And when k is negative i is reduced
Basically means that i and j are clamped to the ends of the list for both positive and negative steps.
0

Actually in case of python string slicing it takes three arguments:

[firstArgument:secondArgument:thirdArgument]

FirstArgument consists in the integer value from where the string get sliced.

SecondArgument tells about the up to where the string being sliced.

ThirdArgument tells about that how much step it get jumped

Example:

a="Hello"
print(a[::2])
# outputs 'hlo' without quoting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.