Tutorial Playlist

Python Tutorial for Beginners

Overview

How to Install Python on Windows?

Lesson - 1

Python OOPs Concept: Here's What You Need to Know

Lesson - 2

Getting Started With Jupyter Network

Lesson - 3

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 4

A Beginner’s Guide To Python Variables

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Learn A to Z About Python Functions

Lesson - 7

The Basics of Python Loops

Lesson - 8

Introduction to Python While Loop

Lesson - 9

Python For Loops Explained With Examples

Lesson - 10

Understanding Python If-Else Statement

Lesson - 11

Introduction to Python Strings

Lesson - 12

Everything You Need to Know About Python Slicing

Lesson - 13

All You Need To Know About Python List

Lesson - 14

Python Regular Expression (RegEX)

Lesson - 15

An Introduction to Python Threading

Lesson - 16

Objects and Classes in Python: Create, Modify and Delete

Lesson - 17

The Best Python Pandas Tutorial

Lesson - 18

A Beginner's Guide To Web Scraping With Python

Lesson - 19

A Handy Guide to Python Tuples

Lesson - 20

How to Easily Implement Python Sets and Dictionaries

Lesson - 21

Everything You Need to Know About Python Arrays

Lesson - 22

An Introduction to Matplotlib for Beginners

Lesson - 23

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 24

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 25

The Best NumPy Tutorial for Beginners

Lesson - 26

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 27

Top 10 Reason Why You Should Learn Python

Lesson - 28

How To Become a Python Developer

Lesson - 29

Top 50 Python Interview Questions and Answers in 2020

Lesson - 30
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.