12/12/2023, 17:23 Everything You Need to Know About Python Slicing
An index is a position of an individual character or element in a list, tuple, or string. The index
value always starts at zero and ends at one less than the number of items.
Fig: Index position
Negative indexes enable users to index a list, tuple, or other indexable containers from the end of
the container, rather than the start.
Want a Top Software Development Job? Start Here!
Full Stack Development-MEAN
EXPLORE PROGRAM
What Is Slicing?
Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the speci c
range of elements by mentioning their indices.
Syntax: Object [start:stop:step]
“Start” speci es the starting index of a slice
“Stop” speci es the ending element of a slice
You can use one of these if you want to skip certain items
Example:
Fig: Python slicing
https://www.simplilearn.com/tutorials/python-tutorial/python-slicing 3/8
12/12/2023, 17:23 Everything You Need to Know About Python Slicing
Note: The search will start at index one (included) and ends at index six (not included).
The following image shows some examples of slicing in a string and tuple:
Fig: Additional slicing examples
Slice with Negative Indices
Negative slicing begins at the end of the list.
Example:
Fig: Slicing using negative indices
Specify Step of the Slicing
You can follow a speci c step in order to skip certain items.
Example:
Fig: Stepped slicing
In the above example, the string ‘SIMPLILEARN’ is sliced from indexes one through seven.
However, because the step size was set as number two, you will receive every second character
in the output starting from the rst index.
Reversing Elements of Data Structure
We can use a negative step to reverse the elements of a data structure.
https://www.simplilearn.com/tutorials/python-tutorial/python-slicing 4/8
12/12/2023, 17:23 Everything You Need to Know About Python Slicing
Example:
Fig: Reversing elements using negative step
Want a Top Software Development Job? Start Here!
Full Stack Development-MEAN
EXPLORE PROGRAM
Slice() Function in Python
The slice() method extracts a section of data and returns it as new data, without modifying it.
This means users can take a speci c range of elements without changing it.
Syntax: slice(start,stop,step)
Example:
Fig: slice() function
Insert and Delete Multiple Items Using Slicing
1. Slice insertion in Python
Users can insert items into a list without replacing other elements.
Example:
https://www.simplilearn.com/tutorials/python-tutorial/python-slicing 5/8
p
12/12/2023, 17:23 Everything You Need to Know About Python Slicing
Fig: Inserting elements using Python slicing
2. Slice deletion in Python
Users can delete multiple items out of the data structure by using a del statement.
Example:
Fig: Delete list items using Python slicing
Note: Tuple object does not support item deletion.
Conclusion
In this article, we discussed two key concepts: Python slicing and indexing. We looked at how
slicing, negative slicing, and step-indexing works through various examples. Both concepts are
essential for understanding Python.
If you have any questions, please leave them in the comments section, and we'll have our experts
answer them for you.
Want to Become a Pro in Web Development?
Web Development skills are in high demand in many industries today and is one of the core tools
used in the exciting elds of development. If you want to boost your career or simply get started,
enroll in our Post Graduate Program In Full Stack Web Developmenttoday!
Find our Full Stack Developer - MEAN Stack Online Bootcamp in top cities:
https://www.simplilearn.com/tutorials/python-tutorial/python-slicing l 6/8
12/12/2023, 17:23 Everything You Need to Know About Python Slicing
Name Date Place
Full Stack Developer - MEAN Cohort starts on 20th Dec 2023,
Your City
Stack Weekend batch
Full Stack Developer - MEAN Cohort starts on 3rd Jan 2024,
Your City
Stack Weekend batch
About the Author
Simplilearn
Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud
Computing, Project Management, Data Science, IT, Software Development, and many other em…
View More
Recommended Programs
Full Stack Web Developer - MEAN Stack Lifetime
Access*
884 Learners
*Lifetime access to high-quality, self-paced e-learning content.
Explore Category
Recommended Resources
Slicers in Excel: The Ultimate Python Interview G
https://www.simplilearn.com/tutorials/python-tutorial/python-slicing 7/8
12/12/2023, 17:23 Everything You Need to Know About Python Slicing
Guide to Help …
© 2009 -2023- Simplilearn Solutions
Disclaimer
PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
https://www.simplilearn.com/tutorials/python-tutorial/python-slicing 8/8