Introduction
The find() function in Python is used to check if a substring is in a string according to the start and end values ​​you set. This function is almost the same as the index() function.
If present, the function will print the lowest index of that substring. Otherwise, it will return -1.
And the article below, we will show you how to use the find() function in Python through several examples as go through below.
Example
str = "Welcome to Python" x = str.find("to") print(x)
Output:
8
Definition
The find() function finds the lowest position of the substring.
If not found, it will return -1.
The syntax
$ string.find(value, start, end)
Parameter Values:
value: substring that you need to find
start: starting value. Default is 0
end: ending value. Default is the end of the string
More examples
Example 1: Find the word “e”
str = "Welcome to Python" x = str.find("l") print(x)
Output:
1
Example 2: Find the word “e” from position 3 to position 10
str = "Welcome to Python" x = str.find("e", 3, 10) print(x)
Output:
6
Example 3: try to find a substring that does not appear
str = "Welcome to Python" x = str.find("a") print(x)
Output:
-1
Conclusion
Hope you understand our tutorial on how to use the find() function in Python.
Thanks for reading!