Using Step in a Slice

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.

How to use Step in Slice?

Slice Function is nothing 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:

  1. 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.
  2. 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.
  3. 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.

  1. Easy to iterate over iterable structures.
  2. Reduces the complexity to perform some complex operations like reverse the list or string.
  3. Makes it easy to perform operations on elements in any sequence without using looping statements.
  4. 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

Run
#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.

Run
#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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

2 comments on “Using Step in a Slice”