Use the [] operator on the tuple to Slice the tuple in Python. If using a positive number, it slices that index from the tuple counting from the left. Or if it’s a negative number, it fetches that index from the tuple counting from the right.
If you want to get a part of the tuple, use the slicing operator.
[start:stop:step].
Read: Slice notation
Example Slice tuple Python
Simple example code.
If we don’t mention the start value the range by default starts from the first term.
my_tuple = ('A', 'B', 'C', 'D')
print(my_tuple[1])
print(my_tuple[-1])
Output:
Ues tuple[start:stop] the start index is inclusive and the stop index
my_tuple = ('A', 'B', 'C', 'D', 'E')
res = my_tuple[3:5]
print(res)
Output: (‘D’, ‘E’)
If the stop value is not used then the range by default ends at the last term.
my_tuple = ('A', 'B', 'C', 'D', 'E')
res = my_tuple[1:]
print(res)
Output: (‘B’, ‘C’, ‘D’, ‘E’)
Do comment if you have any doubts or suggestions on this Python tuple topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.