The find() method returns the lowest index of the substring if it is found in given string. If its is not found then it returns -1.
Syntax :
str.find(sub,start,end)
Parameters :
sub : It’s the substring which needs to be searched in the given string.
start : Starting position where sub is needs to be checked within the string.
end : Ending position where suffix is needs to be checked within the string.
NOTE : If start and end indexes are not provided then by default it takes 0 and length-1 as starting and ending indexes where ending indxes is not included in our search.
Returns:
returns the lowest index of the substring if it is found in given string. If it’s not found then it returns -1.
The find() method is similar to index(). The only difference is find() returns -1 if searched string is not found and index() throws an exception in this case.
CODE 1
word = 'geeks for geeks' # returns first occurrence of Substring result = word.find('geeks') print ("Substring 'geeks' found at index:", result ) result = word.find('for') print ("Substring 'for ' found at index:", result ) # How to use find() if (word.find('pawan') != -1): print ("Contains given substring ") else: print ("Doesn't contains given substring") |
Substring 'geeks' found at index: 0 Substring 'for ' found at index: 6 Doesn't contains given substring
CODE 2
word = 'geeks for geeks' # Substring is searched in 'eks for geeks' print(word.find('ge', 2)) # Substring is searched in 'eks for geeks' print(word.find('geeks ', 2)) # Substring is searched in 's for g' print(word.find('g', 4, 10)) # Substring is searched in 's for g' print(word.find('for ', 4, 11)) |
10 -1 -1 6
Recommended Posts:
- How to find the int value of a string in Python?
- Python | Ways to find all permutation of a string
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- Find frequency of each word in a string in Python
- Find length of a string in python (4 ways)
- Python | Find position of a character in given string
- Python Counter| Find all duplicate characters in string
- Python program to find occurrence to each character in given string
- Python | Ways to find nth occurrence of substring in a string
- Find the first repeated word in a string in Python using Dictionary
- Find all the numbers in a string using regular expression in Python
- Python | Find Mixed Combinations of string and list
- Python Dictionary to find mirror characters in a string
- Python | Find all close matches of input string from a list
- Python Program to find minimum number of rotations to obtain actual string
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python - Remove front K characters from each string in String List
- Python | Merge Tuple String List values to String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : dubey._.ji

