Python: Slice Notation on Tuple

python_tutorials

Introduction

The term slicing in programming usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively.

Python offers an array of straightforward ways to slice not only these three but any iterable. An iterable is, as the name suggests, any object that can be iterated over.

In this article, we’ll go over everything you need to know about Slicing Tuples in Python.

Slicing a Tuple in Python

There are a couple of ways to slice a tuple, most common of which is by using the : operator with the following syntax:

tuple[start:end]
tuple[start:end:step]

The start parameter represents the starting index, end is the ending index, and step is the number of items that are “stepped” over.

If step isn’t explicitly given, the default value is 1.

Let’s go ahead and slice a tuple:

a_tuple = ('You', 'can’t', 'stop',

 

 

To finish reading, please visit source site