Signup/Sign In

Python String index()

Python String index() method is used to return the index of a substring if it is found inside a string.

  • In case if the substring is not there inside the string then this method raises an exception.

  • This method basically returns the index of the first occurrence of the value we are searching in a string.

  • In the case of non-existence of substring inside a string ValueError Exception is raised.

Python String index(): Syntax

Below we have a basic syntax of String index() in Python:

string.index(value, start, end)

Python String index(): Parameters

Given below is a description of Parameters of index() as there are three parameters:

  • value

This is a required parameter and is used to specify the value which is to be searched.

  • start

This is an optional parameter and is used to specify where the search starts; its default value is 0.

  • end

This is an optional parameter used to specify where to end the search. The default value is to the end of the string.

Python String index(): Returned Values

The values returned by this method are as follows:

  • This method returns the first position of the substring found.

  • If substring not found then ValueError exception will be raised.

Python String index(): Basic Example

Below we have an example to show the working of String index() function:

ab = 'We can do whatever we want'
sample = ab.index('w', 5, 20)
print(sample)

The output of the above is given below:


10

Python String find() vs Python String index()

Both these methods are same but there is a major difference in case if the substring does not exist in the string:

  • If we use the python find() method then it will return -1.

  • On the other hand, if we are using the python index() then it will raise ValueError Exception.

Python String index(): Example of value not existing inside the string

Below we have a case where we are looking for the index of a value that does not exist inside the string. Let us see the code snippet with its output:

abc = 'We can do whatever we want'
gfh= abc.index('z')
print(gfh)

Given below is the output


Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
gfh= abc.index('z')
ValueError: substring not found

Python String index(): Without Using start and end Parameter

Below we have an example where we have not specified the value of start and end parameters of index() method. The code snippet is given below:

ab = 'Expecto Patronum'
data = ab.index('num')
print(data)


13

Time For Live Example!

Now let us see the live example given below for index() method:

Summary

In this tutorial, we learned index() method of Strings in Python which is used to return the index of the substring if found. We saw its parameters and a few basic examples; we also differentiate it with find() method of strings further followed by a live example.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.