slice() Function in Python Programming Language

slice() function in python

slice() Function

Slice() Function in Python Programming Language is a built-in function used to iterate through the sequences. Slice function takes three arguments 1st is starting point 2nd is ending point and 3rd for the order to print the element. Sequence data-type in list, tuple, the string is the data-type where we can perform slice operation using slice function.

slice() Function in Python

In Python, we can use slice function to extract a portion of a sequence (such as a string, list, or tuple) by specifying the start and stop indices.

The syntax for slice function: sequence[start:stop]

  • sequence: The sequence (string, list, tuple etc.) from which you want to extract a slice.
  • start: The index where the slice begins.
  • stop: The index where the slice ends.
slice() Function in Python programming language

slice() function for different data-types

slice() function for list

The list is a sequential data-type and is mutable. We can perform operations on the list using the slice built-in function. Slice function in the list takes 3 arguments where one of them is a mandatory field and two are optional. Let’s have a look at some operations on the list using builtin- slice(0 functions.

Python code for slice function on list

Run
arr = ['P','r','e','p','I','n','s','t','a']

#Slicing in list with 2 arguments
a = slice(1,4)
print(arr[a])

#slicing in list with 1 argument
a = slice(2)
print(arr[a])

a = slice(-1)
print(arr[a])

#Slicing in list with 3 arguments
a = slice(1,5,1)
print(arr[a])
Output:
['r', 'e', 'p']
['P', 'r']
['P', 'r', 'e', 'p', 'I', 'n', 's', 't']
['r', 'e', 'p', 'I']

slice() function for Tuple

A tuple is same as list data-type. But the major difference between list and tuple is that tuple is an immutable data-type. Although we can perform some operations on the tuple. Since built-in function takes the arguments and represents the tuple from a starting point to endpoint.

Python code for slice() function on tuple

Run
arr = tuple(['P','r','e','p','I','n','s','t','a'])

#Slicing in list with 2 arguments
a = slice(1,4)
print(arr[a])

#slicing in list with 1 argument
a = slice(2)
print(arr[a])

a = slice(-1)
print(arr[a])

#Slicing in list with 3 arguments
a = slice(1,5,1)
print(arr[a])
Output:
['r', 'e', 'p']
['P', 'r']
['P', 'r', 'e', 'p', 'I', 'n', 's', 't']
['r', 'e', 'p', 'I']

slice() in Strings

String data-type is an immutable data-type in python. But we can perform many operations on strings than on tuples. Strings are iterable and so we can use built-in function Slice() to print of use the sub-part of string according to our need. Lets have a look at some representations of string using slice function.

Python code for slice() function on String

Run
arr = 'PrepInsta' 

#Slicing in string with 2 arguments
a = slice(1,4)
print(arr[a])

#slicing in string with 1 argument
a = slice(2)
print(arr[a])

a = slice(-1)
print(arr[a])

#Slicing in string with 3 arguments
a = slice(1,5,1)
print(arr[a])
Output:
rep
Pr
PrepInst
repI

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