Indexing and slicing
Select one item or a range without confusing positions with lengths.
On this page
The concept
Indexing retrieves one item. Python sequences begin at position 0, and negative indexes count from the end. Slicing uses [start:stop:step]; the stop position is excluded. Missing bounds use the sequence boundary appropriate for the direction. A step of -1 can reverse a sequence.
Indexing outside a sequence raises IndexError. Slicing beyond its end is allowed and returns the available portion. Strings cannot be edited in place: to replace the first character, create a new string from the new prefix plus a slice of the old string. This distinction becomes important when you later study mutable lists.
Worked example
text = "Python"
print(text[0], text[-1])
print(text[0:3])
print(text[:2], text[2:])
print(text[::2])
print(text[::-1])
print(text[:100])
updated = "J" + text[1:]
print(updated, text)Trace the result
- Write P, y, t, h, o, n with indexes 0 through 5 before inspecting text[0] and text[-1].
- text[0:3] selects positions 0, 1, and 2; it does not include position 3.
- The updated variable is a new string. Printing updated and text shows that text still contains its original value.
P n
Pyt
Py thon
Pto
nohtyP
Python
Jython PythonLine-by-line walkthrough
Line 1: text = "Python"
Evaluate 'Python', then bind or update text with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 2: print(text[0], text[-1])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 3: print(text[0:3])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 4: print(text[:2], text[2:])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 5: print(text[::2])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 6: print(text[::-1])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 7: print(text[:100])
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: updated = "J" + text[1:]
Evaluate 'J' + text[1:], then bind or update updated with the result. Assignment itself does not imply a deep copy.
Line 9: print(updated, text)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
An empty string has no element at index 0, but slicing it is valid. Decide whether empty input is allowed before indexing.