s = "0123456789"
print(s[2:-1:-1])
according to me, output of the above question should be "210" BUT it gives nothing please explain to me how?
s = "0123456789"
print(s[2:-1:-1])
according to me, output of the above question should be "210" BUT it gives nothing please explain to me how?
Syntax: sequence [start:stop[:step]]
+---+---+---+---+
|-4 |-3 |-2 |-1 | <= negative indexes
+---+---+---+---+
| A | B | C | D | <= sequence elements
+---+---+---+---+
| 0 | 1 | 2 | 3 | <= positive indexes
+---+---+---+---+
|<- 2:-1:-1 ->| <= extent of the slice: "ABCD"[2:-1:-1] (won't work)
Explanation:
Here in my example "ABCD"[2:-1:-1]
If we interpret it, then it says:
So the solution would be "ABCD"[2::-1]
as someone correctly answered in the comment. This says start from index 2 go till end either beginnig or end based on the steps which is -1 here so beginning.
So same answer to your question print(s[2::-1])
will print 210