What Is Slicing In Python?

What Is Slicing In Python?

Published on October 07 2023

In this tutorial, we will learn about slicing in Python String and list and how to use it efficiently. This tutorial is part of Python's top 25 most commonly asked interview questions.

Definition

  • Slicing is used to access parts of sequences like lists, tuples, and strings.
  • Slicing can be done on strings, arrays, lists, and tuples.

Syntax

  • [start: stop: step] or [start: end: step]
    • start is the starting index from where to slice
    • stop is the ending index or where to sop.
    • step is the number of steps to jump.
    • The default value for a start is 0, a stop is the number of items, and a step is 1.

Example

data = "The Programming Portal"
print(data[4:10])

Output

Output: Progra

To know about it, please refer to the official documentation website - official website.