Lesson 13 of 32By Simplilearn
Last updated on Jul 23, 20207361Python 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:
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.
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]
Example:
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:
Fig: Additional slicing examples
Negative slicing begins at the end of the list.
Example:
Fig: Slicing using negative indices
You can follow a specific 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 first index.
We can use a negative step to reverse the elements of a data structure.
Example:
Fig: Reversing elements using negative step
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:
Fig: slice() function
Users can insert items into a list without replacing other elements.
Example:
Fig: Inserting elements using Python slicing
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.
Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.
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.
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!
Name | Date | Place | |
---|---|---|---|
Full Stack Java Developer | Cohort starts on 15th Feb 2021, Weekend batch | Your City | View Details |
Full Stack Java Developer | Cohort starts on 1st Mar 2021, Weekend batch | Los Angeles | View Details |
Full Stack Java Developer | Cohort starts on 22nd Mar 2021, Weekend batch | Chicago | View Details |
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.
Full Stack Java Developer
Python Training
*Lifetime access to high-quality, self-paced e-learning content.
Explore Category