Python Slice Constructor – Python Slice String & Slicing Tuple
Don't become Obsolete & get a Pink Slip
Follow DataFlair on Google News & Stay ahead of the game
1. Python Slice – Objective
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.
2. 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]
[2, 3, 4]
3. 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.
Have a look at different Python Function
a. 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.
b. Python Slice Example
Let’s take a simple example of Python Slicing.
slice(None, 3, None)
We can index this as:
>>> l=[1,2,3,4,5] >>> l[slice(3)]
[1, 2, 3]
Note that it displays values at the indices 0, 1, and 2. It stops at index 3.
4. 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.
You must learn about Python String
>>> 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.
Do you know about Python Decorators
5. 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.
6. 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.
Let’s revise Python Lambda Expressions
When indexing, what happens when we do not pass one or more of the values?
>>> t[:3] #From 0 to 2
(1, 2, 3)
>>> t[3:] #From 3 to end
>>> t[:] #From beginning to end
(1, 2, 3, 4, 5)
a. Extended Python Slices with a step value
>>> t[::-1] #Reverse
(5, 4, 3, 2, 1)
>>> t[::-2] #Reverse with step=2
(5, 3, 1)
>>> t[:5:2] #Upto index 5, with step=2
(1, 3, 5)
>>> t[:5:-1] #Index 5 to end (already ahead of that), right to left; results in empty tuple
(1, 2, 3, 4, 5)
>>> t[len(t)::-3] #End to front, step=3 right to left
b. Resizing Lists in Python Slices
>>> l=[1,2,3,4,5] >>> l[1:4]
[2, 3, 4]
>>> l[1:4]=[2,3,3.5,4] >>> l
[1, 2, 3, 3.5, 4, 5]
The length of the slice on the right should be equal to that on the left.
c. Deleting Python Slices
We can also use the del keyword to delete a slice.
>>> del l[::4] >>> l
[2, 3, 3.5, 5]
Let’s revise Python Data Structures
So, this was all in Python Slice. Hope you like our explanation.
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. Still, if any confusion in Python Slice, ask freely in the comments. See you tomorrow with a new topic in Python. Till then, keep practicing.
See also –
Python Speech Recognition