Python Slice Constructor – Python Slice String & Slicing Tuple
Free Python course with 25 real-time projects Start Now!!
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.
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]
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(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.
We can index this as:
>>> l=[1,2,3,4,5] >>> l[slice(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)]
- With positive indices-
Like in the previous example, we use positive indices here.
>>> s='helloworld' >>> s[slice(1,6)]
- 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)]
Confused? Here’s how we traverse right to left:
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)]
- Negative Indices-
Let’s traverse right to left.
Indexing to Create Python Slice
We have often sliced lists using [ : ]. Let’s try that one more time.
Remember the previous Python Slicing example? Now take a look at this-
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
>>> t[3:] #From 3 to end
>>> t[:] #From beginning to end
1. Extended Python Slices with a step value
>>> t[::-1] #Reverse
>>> t[::-2] #Reverse with step=2
>>> t[:5:2] #Upto index 5, with step=2
>>> t[:5:-1] #Index 5 to end (already ahead of that), right to left; results in empty tuple
>>> t[len(t)::-3] #End to front, step=3 right to left
2. Resizing Lists in Python Slices
>>> l=[1,2,3,4,5] >>> l[1:4]
>>> l[1:4]=[2,3,3.5,4] >>> l
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
So, this was all in Python Slice. Hope you like our explanation.
Python Interview Questions on Slice Constructor
- What is slicing in Python? Explain with an example.
- How do you slice in Python?
- What is slicing operator in Python?
- Can you slice a tuple in Python?
- How does Python slicing work?
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.