Copyright © tutorialspoint.com

Python String index() Method

previous next


Description:

This method determines if str occurs in string, or in a substring of string if starting index beg and ending index end are given. This method is same as find(), but raises an exception if str is not found

Syntax:

str.index(str, beg=0 end=len(string))

Parameters:

Here is the detail of parameters:

Return Value:

It returns index if found otherwise raises an exception if str is not found.

Example:

#!/usr/bin/python

str = "this is string example....wow!!!";
str = "exam";

print str.index(str);
print str.index(str, 10);
print str.index(str, 40);

This will produce following result:

15
15
Traceback (most recent call last):
  File "test.py", line 8, in 
    print str.index(str, 40);
ValueError: substring not found

shell returned 1

Note: We would see how to handle exceptions in subsequent chapters. So for the time being leave it as it is.


previous next

Copyright © tutorialspoint.com