How to Use the Python slice() Function
In this tutorial, you'll learn how to use the Python slice() function. The slice function returns a slice object representing the set of indices specified by the start, stop, and step arguments.
You can use the Python slice() function to slice lists and tuples.
Python slice() Syntax
The syntax of the slice() function is as follows:
slice(stop)
or:
slice(start, stop[, step])
Python slice() Parameters
- start: The index of the element used as the start of the slice
- stop: The index of the element that should stop just before to finish the slice
- step: The step is a difference between each number in the sequence, and defaults to 1 if not specified.
Note that you can return a sub-list or sub-tuple of a list or tuple using notation, [] with colons between numbers. For example, list[1:4:1].
Examples
Example 1: Slice a List of Strings (1)
The following example illustrates a sample program that slices a list of strings and returns a list of the first three items:
#slice a list of strings. Slice and return the first three fruitsfruits = ["Apple", "Kiwi", "Orange", "Melon", "Watermelon", "Cabbage", "Lettuce"]slice_object = slice(3)slice_fruits = fruits[slice_object]print(slice_fruits)
Result:
['Apple', 'Kiwi', 'Orange']
Here, as you can see, the stop argument set to 3. Since it is a positive value, the program returns a new slice object from the beginning of the list.
Example 2: Slice a List of Strings (2)
The following example illustrates a sample code that slices a list of strings, removes the last two item, and then returns a list of remaining items:
#slice a list of stringsfruits = ["Apple", "Kiwi", "Orange", "Melon", "Watermelon", "Cabbage", "Lettuce"]slice_object = slice(-2)slice_fruits = fruits[slice_object]print(slice_fruits)
Result:
['Apple', 'Kiwi', 'Orange', 'Melon', 'Watermelon']
Here, the stop argument set to -2. Since it is a negative value, the program removes the last two string from the end of the list and returns a new slice object.
Example 3: Slice a List of Strings (3)
The following example illustrates a sample code that slices a list of strings with both the start and stop arguments specified:
#slice a list of stringsfruits = ["Apple", "Kiwi", "Orange", "Melon", "Watermelon", "Cabbage", "Lettuce"]slice_object = slice(4, 5)slice_fruits = fruits[slice_object]print(slice_fruits)
Result:
['Watermelon']
Example 4: Slice a List of Strings (4)
The following example illustrates a sample code that slices a list of strings with three arguments (start, stop, and step) specified.
#slice a list of stringsfruits = ["Apple", "Kiwi", "Orange", "Melon", "Watermelon", "Cabbage", "Lettuce"]slice_object = slice(1, 6, 2)slice_fruits = fruits[slice_object]print(slice_fruits)
Result:
['Kiwi', 'Melon', 'Cabbage']
Example 5: Slice a Tuple of Numbers
The following example illustrates a sample code that slices a tuple of numbers:
numbers = (100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
slice_object = slice(3, 6)
slice_numbers = numbers[slice_object]
print (slice_numbers)
Result:
(400, 500, 600)
Example 6: Slice a String (1)
The following example illustrates a program that slices a string and returns a substring:
#slice a stringstr = "Python Java C++ C# Ruby C PHP JavaScript ASP.NET Django Flask WordPress Laravel"slice_object = slice(10)slice_string= str[slice_object]print(slice_string)
Result:
Python Jav
Example 6: Slice a String (2)
The following example illustrates a program that slices a string and returns a substring:
#slice a stringstr = "Python Java C++ C# Ruby C PHP JavaScript ASP.NET Django Flask WordPress Laravel"slice_object = slice(-10)slice_string= str[slice_object]print(slice_string)
Result:
Python Java C++ C# Ruby C PHP JavaScript ASP.NET Django Flask WordPre
In this tutorial, you've learned how to use the Python slice() function. The slice function returns a slice object representing the set of indices specified by the start, stop, and step arguments.