Slicing an object in Python means slicing the given sequences like String, bytes, tuple, list, range, or any object which supports the sequence protocol ( which implements __getitem__() and __len__() method).
Python slice() is a built-in function that creates the slice object representing the set of indices specified by range(start, stop, step). The slice object is used to determine how to slice the sequence.
You can specify where to start the slicing and where to end. You can also determine the step which allows you to, e.g., slice only every other element.
The syntax of the Python slice() function is the following.
slice(stop) slice(start, stop, step)
The start parameter is optional, and it is the starting integer where the slicing of the object starts.
The stop parameter is required, and it is an integer number specifying at which position to end the slicing.
The step parameter is optional and is an integer value that determines the increment between each index for slicing.
Let’s see the example of the Python slice() method.
# app.py A = ('Bad Bunny', 'Louis Fonsi', 'Daddy Yankee', 'Drake') result = slice(1,3) print(A[result])
The output of the above program is the following.
See in the above example, we have sliced the A object from the index 1 to 3, and we get the elements like Louis Fonsi and Daddy Yankee.
Get substring from a given string using a slice object.
Let’s see the following example of String: we will get a substring from a string.
# app.py A = 'AppDividend' result = slice(3) print(A[result])
Now, see the output below.
Slice String Using Negative Index
In the following example, we will slice a string based on the negative index.
# app.py A = 'AppDividend' result = slice(-3, -1) print(A[result])
In the below example, we have passed the negative index, which means that the execution starts from the end and then goes forward and returns the value.
See the output below.
Python slicing array
To slice an array in Python, use the numpy library. The np.array() function creates an array, and you can slice an array by taking elements from one given index to another given index.
You can pass the slice instead of an index like this: [start: end].
You can also define the step like this: [start: end: step].
If you don’t pass the start parameter, it’s considered 0.
If you don’t pass the end argument, it’s considered the size of an array in that dimension.
If you don’t pass the step parameter, it’s considered 1.
# app.py import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(arr[2:7])
[3 4 5 6 7]
In this example, we are slicing an array from index 2 to 7. Here, one thing to remember is that 2nd index value is not included in the final output, but the last index, which is 7, will be included in the output. That means the start index won’t be included, but the end index will be included.
Slicing 2D Arrays in Python
The syntax for slicing 2D arrays in Python is the following.
<slice> = [start_row:end_row, start_col:end_col]
Let’s slice a 2D array.
# app.py import numpy as np arr = np.array([[1, 3, 5, 7], [2, 4, 6, 8]]) print(arr) b = arr[0:2, 0:3] print(b)
[[1 3 5 7] [2 4 6 8]] [[1 3 5] [2 4 6]]
That’s it for this tutorial.