Python slice() Function Tutorial

Python Slice() Function

This is a detailed tutorial of the Python slice() function. Learn to create slice objects to further slice different data types such as strings & lists.

Python slice() Function

The purpose of the built-in function slice() is to create a slice object which further can be used to actually slice out the different data types such as Strings, Lists, Tuples, etc. The slice object basically contains order or sequence in which any other Python Object has to be sliced. It will be better to explain the usage of the slice() method with the help of examples. So, let’s quickly have a look at the syntax, its parameters, and the return type first.

Syntax

slice() Parameters

The method slice() can take three parameters. Only the second parameter is necessary while the rest two (the first and third one) are optional to be provided as arguments.

  • start is an optional parameter. It is an integer value defining the starting value of the slice object. As it is optional, in case you do not provide it as an argument, it has the default value None.
  • stop is a mandatory parameter. It is also an integer value defining the ending value of the slice object.
  • step is also an optional parameter. The default value of it is None. It is again an integer defining the index of increment between the slicing object values. The value of the step should not be zero.

The parameters by definition might be a bit harder to explain but as you’ll go through the examples section, the purpose of each of these parameters will be cleared to you.

slice() Return Type

The built-in function slice() returns a slice object. These slice objects themselves are just sequences that can be further used to slice the other python objects like the strings, lists, and tuples. For example, if you want to slice string into some particular sequence to generate a new string, you must first make use of the slice() method to first generate a slice object and then that slice object will be used on the string to further generate the actually required sliced string.

Getting confused? Well, keep reading, your confusion will soon be over.

Examples

I’ll first show you how you can create slice objects and then I’ll present some of the examples illustrating the practical usage of the slice() function to slice different strings and other data types.

Example 1. Creating a Slice Object

Here we’re just creating a slice object which we can further use for the slicing purposes.

So, we’ve created three different slice objects. To create the first one, we’ve only passed the only required argument value for the stop parameter. For the creation of the second object, we’ve passed the value for the start and stop parameters as the first and the second arguments.

And at last for the third slice object creation, we’ve passed the values as the first, second and third arguments for all of the parameters start, stop and step respectively. You can make use of the type() method to check that the resultant object belongs to class ‘slice’.

Python Built In Function Slice() Example For Creating Slice Objects

When we print the slice objects, it prints as it is along with the values of all the passed arguments. That’s just how we can create slice objects using the Python built-in function slice(). Let’s find out the practical use-cases in other examples.

Example 2. Generating Sub-String using the slice Object

Generating sub-strings from the main string in a particular sequence is one of the most basic practical usage applications of the slice object and the following example demonstrates this.

Read the above code carefully along with the comments and observe the output. After successful observation, it will be cleared to you how the slice object values are actually helpful in doing the slicing of the main string.

Python String Slicing Using Slice() Method Example

Example 3. Negative Argument Indexes For String slicing using the slice Object

The integer values that are passed as the arguments to the slice() method can also be negative and the following examples illustrate the same for string slicing.

Negative indexing is a bit complex to understand. You must try it with different strings and with different negative index values passed for the different arguments to better understand how it works. Various other functions in python also follow the same approach when provided negative indexes.

Python Slice() Function Negative Index Arguments Example

Example 4. Slicing of Lists & Tuples

As I stated multiple times in this article that the collection based data types such as lists and tuples can also be sliced using the slice object so generated using the slice() function so here’s an example illustrating this concept.

Note. Make sure that the index starts from 0. Therefore 0 refers to the first element of the list or tuple and 3 refers to the fourth element of the list or tuple.

Python Slicing Lists And Tuples Example Using Slice() Function

Example 5. Negative Indexing For List & Tuple Slicing using slice() Function

Let’s provide negative index arguments for tuples and lists as we’ve provided for the strings.

Make sure you read the comments while observing the output for clear understanding.

Python Tuples & Lists Negative Slicing Example

Example 6. Index Syntax For Slicing

You need not to explicitly create slicing objects if you are following the index syntax. You can directly define the values of start, stop and step inside the square brackets notation of a Python Object such as string, separated by colons.

The syntax for the same is PythonObjectToSlice[start:stop:step]

The following example illustrates the slicing on a string as well as list and tuples using the index syntax.

Python Index Syntax For Slicing Of Different Objects Example

We’ve explained this method for generating sub-string in another article and that is linked below.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *