Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Hosted By
Ryan Valles
Founder, Interview Kickstart
Our tried & tested strategy for cracking interviews
How FAANG hiring process works
The 4 areas you must prepare for
How you can accelerate your learnings

The slice() Function in Python

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:

  • What Is the slice() Function in Python and What Does It Do?
  • The slice() Function in Python: Syntax
  • The slice() Function in Python: Example
  • FAQs on the slice() Function in Python

What Is the slice() Function in Python and What Does It Do?

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.

The slice() Function in Python: Syntax


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

Parameters: 

  • start (optional): Start index, or the index from which to start the slicing of object.
  • stop: End index, or the index at which object slicing ends. 
  • step (optional): Step size, or the size of each increment/step between each index when slicing. The default value of the step is one.

Return Type: 

Python slice() function returns a sliced object that contains elements only in the desired range. 

The slice() Function in Python: Example

Here, we take a look at how to use the slice() function in Python the next time you need it:

Code


# 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. 

FAQs on the slice() Function in Python

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.

Ready to Nail Your Next Coding Interview?

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!

Sign up now!

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Hosted By
Ryan Valles
Founder, Interview Kickstart
Our tried & tested strategy for cracking interviews
How FAANG hiring process works
The 4 areas you must prepare for
How you can accelerate your learnings

Recommended Posts

All Posts