Python slice() function

In this article we are going to discuss about the slice() function in python with examples.

slice() function in Python:

Based on the specified range, the slice() method returns a portion of an iterable as an object of the slice class. It works with string, list, tuple, set, bytes, or range objects, as well as custom class objects that implement the sequence methods, __getitem__() and __len__().

Syntax:

slice(stop)
slice(start, stop, step)

Parameters

  • start: This is optional. The index at which the iterable’s slicing starts. None is the default value
  • stop: This is the ending index of slicing.
  • step: This is optional. It is the number of steps to jump/increment.

Python slice() function

Method #1: Using slice() function on Strings

Approach:

  • Give the string as static input and store it in a variable.
  • Slice the given string using the slice() function by passing start, and stop values as arguments to it (default step size is 1).
  • Here it returns the string in the index range of 2 to 9(excluding the last index).
  • Print the result string after slicing.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'Hello this is PythonPrograms'
# Slice the given string using the slice() function by passing start, stop 
# values as arguements to it(default stepsize is 1)
# Here it returns the string in the index range 2 to 9(excluding the last index)
# NOTE: It also considers the space as an index value
sliceObj = slice(2, 10)

# Print the result string after slicing
print("The result string after slicing = ", gvn_str[sliceObj])

Output:

The result string after slicing = llo this

NOTE:

 It also considers the space as an index value

If only one parameter is specified, start and step are assumed to be None.

# Give the string as static input and store it in a variable.
gvn_str = 'Hello this is PythonPrograms'

# Pass only the stop value as an argument to the slice() function 
# to get the portion of the string in the given range
# Here start and step values are considered 0, 1 respectively and 
# we get a string in the index range 0 to 9
sliceObject_1 = slice(10)

# Slice the given string by passing the start, stop, step values as arguments
# slice() function and  store it in a variable.
# Here it returns the string from index 1 to 13(excluding last index) with the step value 2 
sliceObject_2 = slice(1, 14, 2)
 
# Print the  result string 1 after slicing
print("The result string in the index range 0 to 9: ", gvn_str[sliceObject_1])
# Print the  result string 1 after slicing
print("The result string in the index range 1 to 13 with step=2: ", gvn_str[sliceObject_2])

Output:

The result string in the index range 0 to 9: Hello this
The result string in the index range 1 to 13 with step=2: el hsi

Method #2: Using slice() function on Lists

Approach:

  • Give the list as static input and store it in a variable.
  • Pass start, and stop as arguments to the slice() function to get the part of the list (default stepsize is 1) and store it in a variable.
  • Here it returns the list elements in the index range 2 to 6(excluding the last index)
  • Pass only the stop value as an argument to the slice() function to get the portion of the list in the given range.
  • Here start and step values are considered 0, 1 respectively by default and
    we get a list of elements in the index range of 0 to 4
  • Slice the given list by passing the start, stop, and step values as arguments to the slice() function and storing it in a variable.
  • Here it returns list elements from index 1 to 7(excluding the last index) with the step value 2
  • Print the result list elements in the index range 2 to 6
  • Print the result list elements in the index range 0 to 4
  • Print the result list elements in the index range 1 to 7 with step=2.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_list = [1, 3, 6, 8, 9, 4, 10, 14, 18]


# Pass start, stop as arguments to the slice() function to get the part of the list
# (default stepsize is 1) and store it in a variable.
# Here it returns the list elements in the index range 2 to 6(excluding the last index)
sliceObject_1 = slice(2, 7)

# Pass only the stop value as an argument to the slice() function 
# to get the portion of the list in the given range
# Here start and step values are considered 0, 1 respectively bu default and 
# we get a list elements in the index range 0 to 4
sliceObject_2 = slice(5)

# Slice the given list by passing the start, stop, step values as arguments
# to the slice() function and store it in a variable.
# Here it returnslist elements from index 1 to 7(excluding last index) with the step value 2 
sliceObject_3 = slice(1, 8, 2)
 
# Print the result list elements in the index range 2 to 6
print("The result list elements in the index range 2 to 6: ", gvn_list[sliceObject_1]) 
# Print the result list elements in the index range 0 to 4
print("The result list elements in the index range 0 to 4: ", gvn_list[sliceObject_2])
# Print the result list elements in the index range 1 to 7 with step=2
print("The result list elements in the index range 1 to 7 with step=2: ", gvn_list[sliceObject_3])

Output:

The result list elements in the index range 2 to 6: [6, 8, 9, 4, 10]
The result list elements in the index range 0 to 4: [1, 3, 6, 8, 9]
The result list elements in the index range 1 to 7 with step=2: [3, 8, 4, 14]

Method #3: Using slice() function on Tuples

# Give the list as static input and store it in a variable.
gvn_tuple = (1, 3, 6, 8, 9, 4, 10, 14, 18)


# Pass start, stop as arguments to the slice() function to get the part of the 
# given tuple(default stepsize is 1) and store it in a variable.
# Here it returns the tuple elements in the index range 2 to 6(excluding the last index)
sliceObject_1 = slice(2, 7)

# Pass only the stop value as an argument to the slice() function 
# to get the portion of the tuple in the given range
# Here start and step values are considered 0, 1 respectively by default and 
# we get a tuple elements in the index range 0 to 4
sliceObject_2 = slice(5)

# Slice the given tuple by passing the start, stop, step values as arguments
# to the slice() function and store it in a variable.
# Here it returns the tuple elements from index 1 to 7(excluding last index)
# with the step value 2 
sliceObject_3 = slice(1, 8, 2)
 
# Print the result tuple elements in the index range 2 to 6
print("The result tuple elements in the index range 2 to 6: ", gvn_tuple[sliceObject_1]) 
# Print the result tuple elements in the index range 0 to 4
print("The result tuple elements in the index range 0 to 4: ", gvn_tuple[sliceObject_2])
# Print the result tuple elements in the index range 1 to 7 with step=2
print("The result tuple elements in the index range 1 to 7 with step=2: ", gvn_tuple[sliceObject_3])

Output:

The result tuple elements in the index range 2 to 6: (6, 8, 9, 4, 10)
The result tuple elements in the index range 0 to 4: (1, 3, 6, 8, 9)
The result tuple elements in the index range 1 to 7 with step=2: (3, 8, 4, 14)

Method #4: Using Negative Indexing

Python supports “indexing from the end,” or negative indexing.
This means that the last value in a sequence has a -1 index, the second last has a -2 index, etc.
If you wish to select values from the end (right side) of an iterable, you can use negative indexing.

Negative sequence indexes in Python represent positions from the array’s end. The slice() function can return negative values. In that case, the iteration will be performed backward, from end to start.

# Slicing the given list using negative indexing
# Here -1 represents the last element of the list 
gvn_list = [1, 3, 6, 8, 9, 4, 10, 14, 18]
sliceObject = slice(-1, -4, -1)
print("Slicing the given list = ", gvn_list[sliceObject])

# Slicing the given string using negative indexing
gvn_string = "Pythonprograms"
sliceObject = slice(-1)
print("Slicing the given string = ", gvn_string[sliceObject])

# Slicing the given tuple using negative indexing
gvn_tuple = (1, 3, 6, 8, 9, 4, 10)
sliceObject = slice(-1, -6, -2)
print("Slicing the given tuple = ", gvn_tuple[sliceObject])

Output:

Slicing the given list = [18, 14, 10]
Slicing the given string = Pythonprogram
Slicing the given tuple = (10, 9, 6)