Python OOPs


Python SQLite


Examples


Others


Index() method in Python


The index() method in python is used to find the index of the first occurrence of a specified value.

Syntax
string.index(value, start, end)

Parameters

  • value - (Required) The value to search for string.
  • start - (Optional) The index to start the search.
  • end - (Optional) The index to end the search.

The following example shows, how works the index() method.

Example
a = "Good morning to All"
b= a.index("m")
print(b)

Output

5

When searching only between positions 3 and 7, is the first occurrence of the letter 'o' ?

Example
a = "Good morning to All"
x = a.index("o",3,7)
print(x)

Output

6

If value is not found,index() method raise an exception.

Example
a = "Good morning to All"
x = a.index("Y")
print(x)

Output

D:\> 01.py
Traceback (most recent call last):
  File "D:\01.py", line 2, in <module>
    x = a.index("y")
        ^^^^^^^^^^^^
ValueError: substring notfound

Python String Methods