What is Slicing?
Is the way to access a part of a List
, String
, or Tuple
.
We use ObjectName[start:stop:step] to slice where:
- Start: specifies the starting element of a slice (included)
- Stop: specifies the ending element of a slice (not included)
- Step: (optional) specifies the increment between each index for slicing, default to one if not provided
Let's demonstrate with a list:
PYTHON
1# Our list
2my_list = ['Carl Cox', 'Fred again..', 'Sonny Fodera', 'MK', 'Denis Sulta','Boots & Kats']
- Positive Slicing
PYTHON
1my_list[1:4:1] # can also be written as my_list[1:4]

Let's add a step size.
PYTHON
1my_listmy_list[0:5:2]

- Negative Slicing
PYTHON
1my_list[-1:-4:-1]

Again, let's add a step size.
PYTHON
1my_list[-1:-6:-3]
