0
1.5kviews
Explain Indexing and Slicing operations for string manipulation with examples in python.
1 Answer
0
103views

Solution:

Indexing operation:

The indexing syntax of a slice is a shorthand or a better substitute for the slice() as it’s easier to understand and execute.

Python index () method is the same as the find() method except it returns an error on failure.

This method returns the index of the first occurred substring and an error if there is no match found.

Indexing Syntax:

String[start : stop : step] So here, instead of creating a slice object first and then implementing it on the string, we directly use the indexing syntax that performs the same operation.

enter image description here

Output:

enter image description here

Slicing operation:

Python provides us with a method slice () that creates a ‘slice’ object that contains a set of ‘start’ & ‘stop’ indices and step values.

To be specific about the parameters, it is (start, stop, step).

slice (start, stop, step) Both slice() implementations return an object of the format slice(start, stop, end).

enter image description here

Output:

enter image description here

Please log in to add an answer.