Python String find() Method
Searches the string for a specified value and returns the index of the first occurrence of the specified substring. Optional start and end indices can also be specified.
Syntax
Python
Copy Code
string.find(value, start = None, end = None)
Parameters
Parameter | Description |
---|---|
value |
Required. The value to search for. |
start |
Optional. The index at which to start searching. The default is None meaning 0. |
end |
Optional. The index at which to stop searching. The default is None meaning the end of the string. |
Example
Python
Copy Code
str = 'Python is a great programming language for great people.' print(str.find('great')) print(str.find('great', 20)) print(str.find('great', 20, 30))
Output
12 43 -1
Notes: Notice how the index returned is the absolute index of the source string, regardless of the specified start position. Also notice that if the range does not include the substring, find
returns -1.