The find() method is used to retrieve whether the string contains the target string. If it does, it returns the index of the first occurrence of the string; otherwise, it returns -1.
The syntax of the find () method is as follows:
str.find(sub[,start[,end]])
The meaning of each parameter in this format is as follows:
[Example] Use the find() method to retrieve the first occurrence of "e" in the string of "www.freelearningpoints.com".
string = "www.freelearningpoints.com" string.find('e')
The output is:
6
[Example] Manually specify the position of the starting index.
string = "www.freelearningpoints.com" string.find('e', 8)
The output is:
9
[Example] Specify the positions of the start index and end index manually.
string = "www.freelearningpoints.com" string.find('e', 2, -3)
The output is:
6
The string between indexes (2, -3) is "w.freelearningpoints. ", and since it does not contain ".", the return value of the find () method is -1.
Note that Python also provides the rfind () method. The biggest difference from the find () method is that rfind () searches from the right side of the string. Example:
string = "www.freelearningpoints.com" string.rfind('e')
The output is:
9
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises