Menu Close

Python slice() function

In this article, we are going to learn all about the Python slice() function that is used to create the slice object.
In previous tutorials, we have seen lots of Python built-in functions that are very helpful for you.

Python slice() function

Python slice function is a built-in function in Python that is used to create the slice object.
A slice object is used to how to slice a sequence. In the slice object, you can specify, where to slice is start and where to slicing is stop. You can also specify the step of slicing.


The syntax of python slice function is:-

slice(start, stop, step)


Slice function in Python accepts three parameters.

  • start:- Index number, Which refers to the position to start the slicing.
  • stop:- Index number, That refers to the position to end the slicing.
  • step:- An integer number specifying the step of the slicing.

Python slice function example

Here we will take some examples to understand slicing function in Python.

Example 1:

Use slice function in Python with only two parameters, start and stop.

x = ['Python', 'Java', 'CSS', 'Java', 'JavaScript', 'PHP', 'Ruby']
obj = slice(2, 5)


['CSS', 'Java', 'JavaScript']

In above output, item of stop index not included.

Example 2:

Use slice function in Python with three parameters.

x = ['Python', 'Java', 'CSS', 'Java', 'JavaScript', 'PHP', 'Ruby', 'R']
obj = slice(2, 7, 2)


['CSS', 'JavaScript', 'Ruby']


In this article, you have learned all about the Python slice function to slicing the object.
You can also use slicing in Python using the slice operator.
slice function in Python is the best option for slicing the sequence ( list, tuple, set, etc)

If this article helpful for you, please share and keep visiting for further Python tutorials.

Python built-in functions

For more information:- Click Here

Happy Coding
~ Vishvajit Rao

Previous Article
Next Article

Related Posts