Menu Close

Python slice() function

python slice function

In this article, we are going to learn all about the Python slice() function which is used to create the slice object. Slicing in Python is the process of finding some part of a sequence like a list, tuple, set and string, etc. Python slice achieves using index numbers.

Before going to see Python slicing, we will see a little bit about Python indexing and negative indexing with the help of the example because whole Python slicing depends on Python indexing.

Python Indexing

Indexing is the process of accessing the specific element from the iterable ( list, set, string, tuple, etc ). There are two types of indexing available in Python.

  • Python non-negative indexing or normal indexing
  • Python negative indexing

Python non-negative indexing

Python non-negative indexing also called normal Python index starts from left to right and it always starts with index 0 which means the index number of the first element in iterable is 0, the index of the second element is 1, the index of the third element is 2 and so on.


lst = ["Python", "Java", "PHP", "C++", "Dot Net", "JavaScript"]
print("First Element:- ", lst[0])
print("Second Element:- ", lst[1])

Output

First Element:-  Python
Second Element:-  Java

Python negative indexing

Python negative indexing is the process of finding the elements from the last of the iterable. Negative indexing always starts with -1 and so on. As you can see below image.


lst = ["Vishvajit", "Harshita", "Vinay", "Pankaj", "Shalini", "Hashinka"]
print("Last Item:- ", lst[-1])
print("Second Last Item:- ", lst[-2])

Output

Last Item:-  Hashinka
Second Last Item:-  Shalini

I hope you have understood the meaning of python indexing.

slice() function in Python

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

Syntax

The syntax of the Python slice function is:-

slice(start, stop, step)

Parameters

The slice function in Python accepts three parameters.

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

Python slice() function example

Python slice is a way of accessing the items from the iterable ( List, Tuple, String, Set, etc ) within a given range of index numbers. To implement slicing in Python, slice() a built-in function will use which takes three parameters to start, stop and step.

Python also supports non-negative slicing and negative slicing also.

Python non-negative slicing

In Python non-negative slicing, we provide the index numbers to access the items from the iterable within a specific range of index numbers. You have to remember always one thing, Python non-negative slicing always starts with a left to right. As you can see below image.

Example: Using slice() with start and stop parameter

In this example, I will access the items from the list lst within ranges of index 1 and 4. I will provide only the start and stop parameters, step parameter by default sets 1.


lst = ["Python", "Java", "PHP", "C++", "Dot Net", "JavaScript"]
x = slice(1, 4)
print(lst[x])

Output

['Java', 'PHP', 'C++']

In the above output, the item of the stop index is not included in the result.

Example: Using slice() function with start, stop and step parameter

In this example, I will go with the slice() function along with the start, end, and step parameters.


x = ["Python", "Java", "PHP", "C++", "Dot Net", "JavaScript"]
obj = slice(0, 5, 2)
print(x[obj])

Output

['Python', 'PHP', 'Dot Net']

Example: Using slice() function with one parameter

If you call the slice() function with one argument, Then the argument always represents the stop index and the slice always starts with a 0 index.


x = ["Python", "Java", "PHP", "C++", "Dot Net", "JavaScript"]
obj = slice(5)
print(x[obj])

Output

['Python', 'Java', 'PHP', 'C++', 'Dot Net']

Note:- You have to always remember one thing during slicing, The Item of the stop index not include in the result set.

Python negative slicing

Python also supports negative slices. You have to pass a negative index number instead of a positive one. As you can see below example.

Example: Using slice() with three negative parameter


x = ["Vishvajit", "Vishal", "Pankaj", "Vaishali", "Harshita", "Vartika"]
obj = slice(-1, -5, -1)
print(x[obj])

Output

['Vartika', 'Harshita', 'Vaishali', 'Pankaj']

Example: Using slice() function with a single negative parameter


x = ["Vishvajit", "Vishal", "Pankaj", "Vaishali", "Harshita", "Vartika"]
obj = slice(-5)
print(x[obj])

Output

['Vishvajit']

Note:- You have to always remember one thing during the negative slicing, The Python slice function with a single negative parameter always represents the item of a particular index number.

Conclusion

In this tutorial. you have seen all about the Python slice() function to slice a Python iterable such as a List, String, Tuple Set, etc. Python supports both negative slices and positive slices. You can also use slicing in Python using the slice operator ( : ). The slice() function in Python is the best option for slicing the sequence ( list, tuple, set, etc).

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

Python built-in functions


For more information:- Click Here

Thanks for your valuable time… 🧑‍💻🧑‍💻❤️❤️

Python super() function
Python str() function

Related Posts