Everything You Need to Know About Python Slicing

Python is a widely used, high-level, general-purpose programming language—often used to develop GUI applications and web applications. It is incredibly appealing in the discipline of rapid application development because it offers dynamic typing and binding options. In this article, we will be learning about an essential topic in Python programming: Python slicing.

We will cover the following topics in this article:

  • What is an index?
  • What is Python slicing?
  • Slice with negative indices
  • The step-by-step slicing process
  • slice() function in Python
  • Insert multiple list items using slicing
  • Delete items using slicing

What Is an Index?

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.

index-position

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.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

What Is Slicing?

Slicing is the extraction of a part of a string, list, or tuple. It enables users to access the specific range of elements by mentioning their indices. 

Syntax: Object [start:stop:step]

  • “Start” specifies the starting index of a slice 
  • “Stop” specifies the ending element of a slice
  • You can use one of these if you want to skip certain items

Example:

slicing

Fig: 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:

additional

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 specific step in order to skip certain items. 

Example:

stepped

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 first index.

Reversing Elements of Data Structure

We can use a negative step to reverse the elements of a data structure.

Example:

reversing

Fig: Reversing elements using negative step

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 specific range of elements without changing it.

Syntax: slice(start,stop,step)

Example:

slice-function

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:

inserting

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:

deletion

Fig: Delete list items using Python slicing

Note: Tuple object does not support item deletion.

Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.

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 Python?

Python skills are in high demand in many industries today and is one of the core tools used in the exciting fields of data science and data analytics. If you want to boost your career or simply get started, enroll in our Python Training Course today!

About the Author

SimplilearnSimplilearn

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 emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.