How to get sub tuple with slice in Python

Slice a tuple

Slicing means accessing a range of tuple. We can do slicing by providing two indices separated by a colon.

The first value is the start index and the second value is the to index. The start index is inclusive and to index is exclusive.

To slice from the end of a tuple we can omit the second index and use negative value for the first index.


x = (1, 2, 3)#   w  ww . j  av a  2s.co  m
print x[0:2] 

aTuple = (1, 2, 3, 4, 5,6,7,)
print aTuple[1:4]
print aTuple[:3]
print aTuple[3:]

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary