Slicing allows us to extract parts of sequences like strings, lists and tuples. While normal slicing uses positive indices, Python also supports negative slicing, which makes it easier to work with elements starting from the end of a sequence.

Negative Indexing
Python uses negative indices to count elements from the end.
- -1 refers to the last element
- -2 refers to the second-last element
- -3 refers to the third-last element and so on.
This feature is especially useful when you don’t know exact length of a sequence but still want to access elements from the end.
Example: Here we access elements from the end of a string using negative indices.
text = "Python"
print(text[-1])
print(text[-3])
Output
n h
Explanation:
- text[-1] -> returns "n", last character.
- text[-3] -> returns "h", third-last character.
Ways to Perform Negative Slicing
Negative slicing can be done in two ways:
1. Using Colon (:) Operator: colon (:) operator accepts three optional parameters
Syntax:
sequence[start : end : step]
Parameters:
- start: Starting index of the slice (can be negative or positive).
- end: Ending index of the slice (exclusive, not included).
- step: Number of steps to move (use -1 to reverse).
Example: Here we slice a string into left, middle and right parts using negative indices.
text = "PythonProgramming"
left = text[:-11]
middle = text[-11:-3]
right = text[-3:]
print(left)
print(middle)
print(right)
Output
Python Programm ing
Explanation:
- text[:-11] extracts the first part of the string, "Python".
- text[-11:-3] extracts the middle part, "Program".
- text[-3:] extracts the last three characters, "ing".
2. Using slice(): Python provides slice() function to achieve the same result in a more explicit way.
Syntax:
slice(start, stop, step)
Parameters:
- start: Starting index (can be None or negative).
- stop: Ending index (exclusive).
- step: Step value (negative step reverses the sequence).
Example: Here we use the slice() function to achieve the same slicing as before.
text = "DataScience"
left = text[slice(-7)]
middle = text[slice(-7, -3)]
right = text[slice(-3, None)]
print(left)
print(middle)
print(right)
Output
Data Scie nce
Explanation:
- slice(-7) extracts the characters from the beginning of the string up to index -7, giving "Data".
- slice(-7, -3) extracts the characters from index -7 to -3 (excluding -3), giving "Scie".
- slice(-3, None) extracts the last three characters of the string, giving "nce".
Slicing List
In this code, we create a list called items. Using negative slicing, we extract last four elements as vegetables and two elements before them as fruits.
li = ["pen", "pencil", "eraser", "apple", "guava", "ginger","Potato", "carrot", "Chilli"]
a = li[-4:]
print(a)
b = li[slice(-6, -4)]
print(b)
Output
['ginger', 'Potato', 'carrot', 'Chilli'] ['apple', 'guava']
Reverse List
Here, we reverse entire list by passing -1 as the step value. The first method uses normal slicing and second uses slice() function with None for start and end and -1 as step.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
rev = num[::-1]
print(rev)
rev2 = num[slice(None, None, -1)]
print(rev2)
Output
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Slicing Alternates in the List
In this example, we extract alternate elements from the list in reverse order. By passing -2 as the step, we skip one element while moving backwards.
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
alt = num[::-2]
print(alt)
alt2 = num[slice(None, None, -2)]
print(alt2)
Output
[10, 8, 6, 4, 2] [10, 8, 6, 4, 2]
Slicing Tuples
This code demonstrates negative slicing with tuples. We access last three elements, select a middle range, reverse the tuple and also extract alternate elements in reverse.
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9)
s = tup[-3:]
print(s)
s = tup[-5:-2]
print(s)
rev_tup = tup[::-1]
print(rev_tup)
s = tup[::-2]
print(s)
Output
(7, 8, 9) (5, 6, 7) (9, 8, 7, 6, 5, 4, 3, 2, 1) (9, 7, 5, 3, 1)