Tutorials Point


  Python Basics
  Python Advanced
  Python Useful References
  Python Useful Resources
  Selected Reading

© 2011 TutorialsPoint.COM


  Home     References     Discussion Forums     About TP  

Python String index() Method


previous next AddThis Social Bookmark Button


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:

  • 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.

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 Printer Friendly



  

Advertisement

Online Image Processing

Indian Baby Names