Python String Index() Method Syntax

Syntax:  string_obj.index(substring, start, end)

Parameters: 

  • substring: The string to be searched for.
  • start (default : 0) : This function specifies the position from where the search has to be started. 
  • end (default: length of string): This function specifies the position from where the search has to end.

Return:  Returns the first position of the substring found.

Exception:  Raises ValueError if the argument string is not found or the index is out of range.

Python String index() Method

Python String index() Method allows a user to find the index of the first occurrence of an existing substring inside a given string in Python.

Similar Reads

Python String Index() Method Syntax

Syntax:  string_obj.index(substring, start, end) Parameters:  substring: The string to be searched for. start (default : 0) : This function specifies the position from where the search has to be started.  end (default: length of string): This function specifies the position from where the search has to end. Return:  Returns the first position of the substring found. Exception:  Raises ValueError if the argument string is not found or the index is out of range....

Python String Index() Method Example

Here the first character of ‘and’ in string random is ‘a’ and the index of ‘a’ is 1, so the output is also 1....

Practical Application

...