Interview Kickstart has enabled over 21000 engineers to uplevel.
Python supports object-oriented programming and has a concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. An integral part of Python are its built-in functions.
We've written a series of articles to help you learn and brush up on the most useful Python functions. In this article, we’ll learn about Python's slice() function and how to use it.
If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Python Exit commands, and Type and Isinstance In Python for more content on Python coding interview preparation.
Having trained over 10,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!
At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.
Want to nail your next tech interview? Sign up for our FREE Webinar.
In this article, we’ll cover:
Python’s slice() function returns a slice object that specifies how to slice a sequence. We can specify where to start and end the slicing as well as the slicing step size. The sequence of objects to be sliced can be of any type, such as tuple, string, bytes, list, range, the object that implements __getitem__() and __len__(), etc.
slice(start, stop, step)
slice(stop)
Parameters:
Return Type:
Python slice() function returns a sliced object that contains elements only in the desired range.
Here, we take a look at how to use the slice() function in Python the next time you need it:
# Using slice() in Python 3 example
# String slicing
stringExample = "InterviewKickstart."
print("Original string: ")
print(stringExample)
stringSlice1 = slice(8)
stringSlice2 = slice(1, 8, 2)
print("\nString sliced: ")
print(stringExample[stringSlice1])
print(stringExample[stringSlice2])
# List slicing
listExample = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("\nOriginal list: ")
print(listExample)
listSlice1 = slice(9)
listSlice2 = slice(1, 9, 3)
print("\nList sliced: ")
print(listExample[listSlice1])
print(listExample[listSlice2])
# Tuple slicing
tupleExample = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tupleSlice1 = slice(9)
tupleSlice2 = slice(1, 9, 3)
print("\nTuple sliced: ")
print(tupleExample[tupleSlice1])
print(tupleExample[tupleSlice2])
# Slicing with negative indices (negative indices are counted backward, from the end)
print("\n\nSlicing with negative indices:")
# String slicing
stringExample = "InterviewKickstart."
print("\nOriginal string: ")
print(stringExample)
stringSlice1 = slice(-8)
stringSlice2 = slice(-1, -8, -2)
print("\nString sliced:")
print(stringExample[stringSlice1])
print(stringExample[stringSlice2])
# List slicing
listExample = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("\nOriginal list: ")
print(listExample)
listSlice1 = slice(-9)
listSlice2 = slice(-1, -9, -3)
print("\nList sliced: ")
print(listExample[listSlice1])
print(listExample[listSlice2])
# Tuple slicing
tupleExample = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
tupleSlice1 = slice(-9)
tupleSlice2 = slice(-1, -9, -3)
print("\nTuple sliced: ")
print(tupleExample[tupleSlice1])
print(tupleExample[tupleSlice2])
Output
Original string:
InterviewKickstart.
String sliced:
Intervie
neve
Original list:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List sliced:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 5, 8]
Tuple sliced:
(1, 2, 3, 4, 5, 6, 7, 8, 9)
(2, 5, 8)
Slicing with negative indices:
Original string:
InterviewKickstart.
String sliced:
InterviewKi
.rtk
Original list:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List sliced:
[1]
[10, 7, 4]
Tuple sliced:
(1,)
(10, 7, 4)
Found this article helpful? You can learn about more Python functions on the learn page.
Q1. How do we slice an input in Python?
You can slice an input in Python using the slice() function.
Q2. What does the slice() function in Python return?
The slice() function returns a slice object which specifies how to slice the sequence.
Q3. How is a slice notation passed in Python?
The function slice() in Python takes up to three parameters: start index, end index, and step in order to slice a sequence. It returns a slice object.
Q4. Does slice() raise an error for out of bound indices?
No, slice() doesn't raise an error if both start and stop indices are out of bounds in the context of the sequence length. It just returns an empty sequence.
Q5. How does Python handle degenerate slices for strings?
In Python’s slice() function, an index larger than the string size is replaced by the string size. If the end index is smaller than the start index, we get an empty string.
Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
Attend our webinar on
"How to nail your next tech interview" and learn