What is indexing and slicing in python

An index is how to identify a particular position in a string, for example.

string1 = ‘firstexample’

if you have the following input:

string1[0]

the output will be

‘f’

The index for the python language starts at 0.

Slicing in python is a function and can be thought of as the following:

slice(starting_index, stopping_index, step_to_take)

For example if you had the following input:

slicer = slice(1,7,2)

print(string1[slicer])

Then the output would be as follows:

isea

Of note, slicer could be as follows:

slicer = slice(-1,-4, -1)

This causes the index to count downward instead of upward and starts the index from the end of the string instead.

Best wishes,

Corey