Python slice()

The slice() in Python is a built-in function that returns a slice object and slices any sequence such as string, tuple, list, bytes, range.

slice() Syntax 

The syntax of the slice() is:

slice(start, stop, step)

slice() Parameters

The slice() method can take three parameters.

  • start (optional) – Starting integer where the slicing of the object starts. If omitted, defaults to None.
  • stop – Ending index where the slicing of object stops. The slicing stops at index stop -1 (last element).
  • step (optional) – An optional argument determines the increment between each index for slicing. If omitted, defaults to None.

Note: If only one parameter is passed, then both start and step will default to None.

slice() Return Value

The slice() method returns a sliced object containing elements in the given range. 

Note: We can use slice with any object which supports sequence protocol which means an object that implements __getitem__() and __len()__ method.

Example 1: Python slice string Get substring using slice object

# Python program to demonstrate slice() operator

# String slicing
str = 'ItsMyPythonCode'
s1 = slice(3)
s2 = slice(5, 11,1)

print(str[s1])
print(str[s2])

Output

Its
Python

Example 2: Get substring using negative index

 In Python, negative sequence indexes represent positions from the end of the array. The slice() function can take negative values, and in the case of a negative index, the iteration starts from end to start.

# Python program to demonstrate slice() operator

# String slicing
str = 'ItsMyPythonCode'
s1 = slice(-4)
s2 = slice(-5, -11,-1)

print(str[s1])
print(str[s2])

Output

ItsMyPython
nohtyP

Example 3: Python slice list or Python slice array

# Python program to demonstrate slice() operator

# List slicing
lst = [1, 2, 3, 4, 5]
s1 = slice(3)
s2 = slice(1, 5, 2)

print(lst[s1])
print(lst[s2])


# Negative list slicing

s1 = slice(-3)
s2 = slice(-1, -5, -2)

print(lst[s1])
print(lst[s2])

Output

[1, 2, 3]
[2, 4]
[1, 2]
[5, 3]

Example 4: Python slice tuple

# Python program to demonstrate slice() operator

# Tuple slicing
tup = (1, 2, 3, 4, 5)
s1 = slice(3)
s2 = slice(1, 5, 2)

print(tup[s1])
print(tup[s2])


# Negative Tuple slicing

s1 = slice(-3)
s2 = slice(-1, -5, -2)

print(tup[s1])
print(tup[s2])

Output

(1, 2, 3)
(2, 4)
(1, 2)
(5, 3)
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Count()

Python String count()

Table of Contents Hide count() Syntaxcount() Parameter count() Return ValueExample 1: Count the number of occurrences of a given substring without optional argumentsExample 2: Count the number of occurrences of a…
View Post
Calculate Euclidean Distance In Python

Calculate Euclidean Distance in Python

Table of Contents Hide How to Calculate Euclidean Distance in Python?Method 1: Using linalg.norm() Method in NumPyMethod 2: Using dot() and sqrt() methodsMethod 3: Using square() and sum() methodsMethod 4:…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post