The process of getting a sub-sequence of a sequence by giving a starting and ending index is known as slicing. The colon: operator is used in Python to perform slicing.
Here’s the syntax:
Where start_index is the index of the sub-sequence's first element and end_index is the index of the sub-sequence's last element (excluding the element at end_index). To slice a sequence, use square brackets [] separated by a comma with the start and end indices.
Here’s an example -
Here, we used slicing in the preceding code to access a sub-sequence of my_list comprising the second and third entries.
In a slice, you can also omit the start_index or the end_index to get all the components from the beginning or end of the sequence.
Here’s an example -
For more details, visit our Python blogs.