Python slice() Function



The Python slice() function is a built-in function that returns a slice object that can be used for slicing sequences, such as lists, tuples, strings, etc.

This function helps in performing several operations which include finding a substring from a given string and extracting a subsection from a collection.

The term slicing is defined as the process of extracting a certain portion of the specified sequence object.

Syntax

Below is the syntax of the Python slice() function −

slice(start, end, step)

Parameters

The Python slice() function accepts three parameters, all of which are optional −

  • start − This parameter accepts an integer value that represents a position from which we need to start slicing. Its default value is 0.

  • end − It also accepts an integer value representing the end position of slicing.

  • step − It specifies the required increment between the slicing index. The default value is 1.

Return Value

The Python slice() function returns a new sliced object.

Examples

In this section, we will see some examples of slice() function −

Example 1

The following example shows the usage of Python slice() function. Here, we are defining a string and trying to extract its sub-string using the slice() function.

orgnlStr = "Simply Easy Learning Tutorials Point"
subStr = orgnlStr[slice(21, 36)]
print("Printing the sub-string of the given string:", subStr)

When we run above program, it produces following result −

Printing the sub-string of the given string: Tutorials Point

Example 2

Slicing can be done by passing the start, end and step parameters, which allows users to get the portion of a list from a starting index to an ending index with a specific increment. In the code below, we are accessing items of a list from the second index to the sixth with an increment of 2 positions.

orgnList = [22, 44, 66, 88, 108, 118]
newSlicedList = orgnList[slice(2, 6, 2)]
print("Printing the newly sliced list:", newSlicedList)

Following is an output of the above code −

Printing the newly sliced list: [66, 108]

Example 4

Since the tuple is also a sequence object, we can use slice() function with tuples as shown in the below code.

orgnlTup = ("Simply", "Easy", "Learning", "Tutorials", "Point")
newSlicedTup = orgnlTup[slice(0, 3)]
print("Printing the newly sliced tuple:")
print(newSlicedTup)

Output of the above code is as follows −

Printing the newly sliced tuple:
('Simply', 'Easy', 'Learning')

Example 5

If we pass a negative index number to the slice() function, the indices will be counted from the end of the sequence, which allows us to slice sequences in reverse order. The following code illustrates how to reverse a given string using the slice() function.

orgnlStr = "TutorialsPoint"
newRevStr = orgnlStr[slice(None, None, -1)]
print("Printing the string in reverse order:")
print(newRevStr)

Following is an output of the above code −

Printing the string in reverse order:
tnioPslairotuT
python_built_in_functions.htm
Advertisements