Copyright © tutorialspoint.com
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
str.index(str, beg=0 end=len(string)) |
Here is the detail of parameters:
str: specifies the string to be searched.
start : starting index, by default its 0
end : ending index, by default its equal to the lenght of the string.
It returns index if found otherwise raises an exception if str is not found.
#!/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 |
Note: We would see how to handle exceptions in subsequent chapters. So for the time being leave it as it is.
Copyright © tutorialspoint.com