Using Step in a Slice
Step in Slice
We can perform many operations on a list object Using Step in a Slice function in Python. Slice function is operable on list structures and it takes exactly 3 arguments where the 3rd argument is the step perimeter. Slicing on the string can be performed with or without using this argument.
To learn more in Python programming language click on the button below
How to use Step in Slice?
Slice function is no something like a built-in function it is just an operation performed on list structures or on the structures which are iterable. Some basic operations like printing a substring from a known starting point to an ending point, or printing the whole string starting after the specific index etc. It exactly takes Three arguments as follows:
- Start:- It is the first argument that we pass when we use the slice function. It takes the index from where you want to start printing or iterating the list or an iterable structure.
- Stop:- It is the second argument that we need to pass when we use the slice function over any list or iterable structure. It takes the ending point or the point up to where you want to access the element.
- Step:- It is the third argument that we pass while iterating through any list or iterable structure, which is also an optional argument. We will be understanding more about the argument in detail below.
Advantages of using Step argument in Slice function
Every operation we perform on some structure has some advantages Lets have a look at Some of the advantages of using step function in Python programming language.
- Easy to iterate over iterable structures.
- Reduces the complexity to perform some complex operations like reverse the list or string.
- Makes it easy to perform operations on elements in any sequence without using looping statements.
- It is an optional argument, so compiler never throws an error if we forget to pass the value.
Python Code to use Step argument in Slicing
#initialize a string arr = 'PrepInsta' #using step argument without start and stop #this will print the string as it is print(arr[::1]) #This will print the string in reverse order print(arr[::-1]) #This will print the string elements with even index print(arr[::2]) #This will print the string with odd indexes print(arr[::3]) #This will print the array element in reverse order with even indexes print(arr[::-2])
Output: PrepInsta atsnIperP PeIsa Pps asIeP
In the above code snippet, we have seen the usage of step argument without the rest two start and stop perimeter, Now let’s use step argument with all the perimeter.
#initialize a string arr = 'PrepInsta' #using step argument without start and stop #this will print the string as it is print(arr[0:9:1]) #This will print the string in reverse order #startring from the index 2 to ending point 6 ar = arr[2:7] print(ar[::-1]) #This will print the string elements with even index #Starting from 2 to 7 print(arr[2:8:2]) #This will print the string with odd indexes #Starting from index 1 to 6 print(arr[1:7:3]) #This will print the array element in reverse order with even indexes ar = arr[0:9] print(ar[::-2])
Output: PrepInsta snIpe eIs rI asIeP