/  Index() method in Python String Module

Index() method in Python String Module


Index method in string module helps to identify the index position of the desired word and gives its indexing position as the output.

If the desired word is not identified it raises an error as an exception.


Syntax:

str. Index(sub [, start[,  end] ])


Parameters:

Index method has 3 parameters.

  1. Sub: this is the substring which needs to be identified in the string
  2. Start: this is the point from where the substring has to be identified in the string. This is an optional parameter
  3. End: this is the point where the search for the substring has to be stopped. this is also an optional parameter.

Index method can be used in 2 ways


Example-1: Index method using only sub parameter

txt = "Geneva is surrounded by Alps mountains."
x = txt.index("is")
print(x)


Output:


Example-2: Index method using sub, start and end parameters

text = "French is widely used language in Geneva."
x = text.index("widely",9, 17)
print(x)


Output: