Python Slice Constructor – Python Slice String & Slicing Tuple

Python course with 57 real-time projects - Learn Python

Today, in this Python Tutorial, we will discuss Python Slice. First, we will see the meaning of Slicing in Python.

Moreover, we will learn Python Slice() function with syntax and example. Also, we will see Python String and Tuples Slicing.

At last we will discuss indexing to create Slice in Python.

Python Slice | Python Slice() Constructor

Python Slice | Python Slice() Constructor

What is Python Slice?

On our way up the learning curve for Python, we only need to deal with Python iterables not so huge.

But when building an application with it, not always do we need to retrieve the entire iterable.

In such cases, slicing is useful as it lets us choose what to see and focus on. This aids readability and implements abstraction.

To slice a iterable, we use the slicing operator, that is [ ]. To separate the start, stop, and step values, we use the colon ( : ).

Say you want to declare a list with the values 1,2,3,4,5, and only want to see values 2,3, and 4. You can slice the list for that.

>>> list=[1,2,3,4,5]
>>> list[1:4]

Output

[2, 3, 4]

The Python Slice() Function

slice() is a constructor that creates a Python Slice object to represent the set of indices that range(start, stop, step) specifies.

With this, we can slice a sequence like a string, a tuple, a list, a range object, or a bytes object.

These are all objects that support sequence protocols and implement __getitem__() and __len__(). The slice() function returns a Python Slice Object.

The Syntax of Python Slice

Let’s talk about the syntax of Slicing in Python first:

slice(stop)
slice(start, stop, step)

What are these parameters? Let’s see:

  • start- Where to begin slicing (Optional)
  • stop- Where to stop slicing+1
  • step- How much to increment between each index (Optional)

You’ll see that we have two syntaxes.

When we provide only one parameter value, it takes it to be the stop value. This means to start and step are set to None.

Python Slice Example

Let’s take a simple example of Python Slicing.

>>> slice(3)

Output

slice(None, 3, None)

We can index this as:

>>> l=[1,2,3,4,5]
>>> l[slice(3)]

Output

[1, 2, 3]

Note that it displays values at the indices 0, 1, and 2. It stops at index 3.

Python String Slice

We can slice a string in Python using the Python slice() method. We can also specify the interval.

Slicing a string may give us a substring when the step size is 1.

>>> s='helloworld'
>>> s[slice(1,6,2)]

Output

‘elw’
  • With positive indices-

Like in the previous example, we use positive indices here.

>>> s='helloworld'
>>> s[slice(1,6)]

Output

‘ellow’
  • With negative indices-

But like we’ve seen with lists earlier, we can pass negative indices too. These are what it traverses from the right.

To read the same value right to left, we do:

>>> s='helloworld'
>>> s[slice(-5,-10,-1)]

Output

‘wolle’

Confused? Here’s how we traverse right to left:

Python Slice

Python String Slice

We index everything from letters to digits and from spaces to characters.

Python Slicing Tuples

Now let’s take a look at performing Python Slicing on tuples. Check the following code:

  • Positive Indices-
>>> t=(1,2,3,4,5)
>>> t[slice(2,4)]

Output

(3, 4)
  • Negative Indices-

Let’s traverse right to left.

>>> t[slice(-1,-5,-2)]

Output

(5, 3)

Indexing to Create Python Slice

We have often sliced lists using [ : ]. Let’s try that one more time.

Python Slice

Indexing to Create Python Slicing

Remember the previous Python Slicing example? Now take a look at this-

>>> t[-1:-5:-2]

Output

(5, 3)

So we concur that slicing is a way to choose indices for elements to show.

What slice() really does is give us indices for those. We can use the slice() function as a subscript.

When indexing, what happens when we do not pass one or more of the values?

>>> t[:3]    #From 0 to 2

Output

(1, 2, 3)
>>> t[3:]    #From 3 to end

Output

(4, 5)
>>> t[:]    #From beginning to end

Output

(1, 2, 3, 4, 5)

1. Extended Python Slices with a step value

>>> t[::-1]    #Reverse

Output

(5, 4, 3, 2, 1)
>>> t[::-2]    #Reverse with step=2

Output

(5, 3, 1)
>>> t[:5:2]    #Upto index 5, with step=2

Output

(1, 3, 5)
>>> t[:5:-1]    #Index 5 to end (already ahead of that), right to left; results in empty tuple

Output

()
>>> t

Output

(1, 2, 3, 4, 5)
>>> t[len(t)::-3]    #End to front, step=3 right to left

Output

(5, 2)

2. Resizing Lists in Python Slices

>>> l=[1,2,3,4,5]
>>> l[1:4]

Output

[2, 3, 4]
>>> l[1:4]=[2,3,3.5,4]
>>> l

Output

[1, 2, 3, 3.5, 4, 5]

The length of the slice on the right should be equal to that on the left.

3. Deleting Python Slices

We can also use the del keyword to delete a slice.

>>> del l[::4]
>>> l

Output

[2, 3, 3.5, 5]

So, this was all in Python Slice. Hope you like our explanation.

Python Interview Questions on Slice Constructor

  1. What is slicing in Python? Explain with an example.
  2. How do you slice in Python?
  3. What is slicing operator in Python?
  4. Can you slice a tuple in Python?
  5. How does Python slicing work?

Conclusion

Hence, in this Python Slice Tutorial, we saw the meaning of Slicing in Python.  Moreover, we discussed Python Slice() object and Python Slice function.

Also, we learned about Python Slice String. 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

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