Meaning of [::-1] in Python Slicing

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.

ParameterDescriptionDefault Value
startStarting index of the slice (inclusive)0
endEnding index of the slice (exclusive)Length of sequence
stepStep size between elements in the slice1

Now let's take an example of [::-1] with string



Result ==> nhoJ


sequence[::-1]Reversed sequenceReturns 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]



Conclusion: tl;dr

[::-1] reverses the sliced string, list or tuple!

Example of colon colon -1 slicing in Python

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap