Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between indexing and slicing in Python
In this article, we will explain the differences between indexing and slicing in Python. Both are fundamental operations for working with sequence data types.
Indexing and slicing are applicable only to sequence data types. The order in which elements are inserted is preserved in sequence types, allowing us to access elements via indexing and slicing.
Python's sequence types include list, tuple, string, range, bytes, and byte arrays. Indexing and slicing are applicable to all of these types.
Indexing
The term indexing refers to accessing a single element of a sequence based on its position within the sequence.
Indexing begins from 0. The first element in the sequence is represented by index 0.
Negative indexing begins from -1. The last element in the sequence is represented by index -1.
Each character in a string corresponds to an index number, and each character can be accessed by its index number. There are two ways to access characters in a string:
Accessing string characters using positive indexing
Accessing string characters using negative indexing
<b>T</b> <b>U</b> <b>T</b> <b>O</b> <b>R</b> <b>I</b> <b>A</b> <b>L</b> <b>S</b>
Positive Indexing 0 1 2 3 4 5 6 7 8
Negative indexing -9 -8 -7 -6 -5 -4 -3 -2 -1
Positive Indexing Example
In this case, we pass a positive index in square brackets. The index number sequence begins with 0.
# input string
input_string = "Hello tutorialspoint python"
print("0th index character:", input_string[0])
print("7th index character:", input_string[7])
print("12th index character:", input_string[12])
0th index character: H 7th index character: u 12th index character: a
Negative Indexing Example
We pass the negative index in square brackets. The index number starts at -1 (representing the last character).
# input string
input_string = "Hello tutorialspoint python"
print("Last index character:", input_string[-1])
print("6th index character from last:", input_string[-6])
Last index character: n 6th index character from last: p
Indexing in Lists
# input list
input_list = [1, 4, 8, 6, 2]
print("Element at index 2:", input_list[2])
print("Last element of input list:", input_list[-1])
Element at index 2: 8 Last element of input list: 2
IndexError Example
When we attempt to use an index that doesn't exist or is too large, Python throws an IndexError.
# input list
input_list = [1, 4, 8, 6, 2]
# This will throw an IndexError
print("Element at index 10:", input_list[10])
IndexError: list index out of range
Slicing
The term slicing refers to obtaining a subset of elements from a sequence based on their indices. Slicing returns a new sequence containing the selected elements.
Syntax
sequence[start : end : step]
Parameters
- start - Index from where to start (inclusive)
- end - Ending index (exclusive)
- step - Number of jumps/increment between elements
String Slicing Example
# input string
input_string = "Hello tutorialspoint python"
print("First 4 characters:", input_string[:4])
print("Characters from index 1 to 10:", input_string[1:10:2])
print("Reverse order from -1 to -10:", input_string[-1:-10:-2])
First 4 characters: Hell Characters from index 1 to 10: el uo Reverse order from -1 to -10: nhy n
Tuple Slicing Example
# Input tuple
given_tuple = ("Welcome", "this", "is", "TutorialsPoint", "Website", 10)
print("Tuple slicing from index 1 to 6:", given_tuple[1:6])
print("Tuple slicing till index 3:", given_tuple[:3])
print("Tuple slicing from index 2:", given_tuple[2:])
print("Complete tuple:", given_tuple[:])
print("Reversed tuple:", given_tuple[::-1])
Tuple slicing from index 1 to 6: ('this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing till index 3: ('Welcome', 'this', 'is')
Tuple slicing from index 2: ('is', 'TutorialsPoint', 'Website', 10)
Complete tuple: ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)
Reversed tuple: (10, 'Website', 'TutorialsPoint', 'is', 'this', 'Welcome')
Comparison
| Aspect | Indexing | Slicing |
|---|---|---|
| Return Value | Single element | New sequence (list/tuple/string) |
| Out-of-range Behavior | Throws IndexError | Handles gracefully (returns empty sequence) |
| Syntax | sequence[index] | sequence[start:end:step] |
| Assignment | Can assign single element | Must assign iterable |
| Length Change | Cannot change sequence length | Can change sequence length |
Conclusion
Indexing retrieves single elements using specific positions, while slicing extracts subsequences using ranges. Understanding both concepts is essential for effective Python programming with sequences.
