The [::-1] can be used in Python as a concept of slicing with strings, lists or tuples, before you know what it is let's take a look at the slicing syntax.
Syntax
sequence[start:stop:step]
| Parameter | Description | Default Value |
|---|---|---|
| start | Starting index of the slice (inclusive) | 0 |
| end | Ending index of the slice (exclusive) | Length of sequence |
| step | Step size between elements in the slice | 1 |
Now let's take an example of [::-1] with string
name="John"
print(name[::-1])
Result ==> nhoJ
string[::-1]
Slice Result Description sequence[::-1] Reversed sequence Returns a new sequence with items in reverse order.
Now let's see an example with a list
numbers = [1, 2, 3, 4, 5]
reversed_numbers = numbers[::-1]
print(reversed_numbers)
Result:
[5,4,3,2,1]

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!