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.
Here at LinuxAPT, we shall look into how to use the find() function in Python through several examples.
The find() function finds the lowest position of the substring.
If not found, it will return -1.
It's syntax is given below:
$ string.find(value, start, end)
find() Function Parameter Values include:
1. Take a look at the below function:
str = "Welcome to Python"
x = str.find("to")
print(x)
The Output will give:
8
2. Find the word "e":
str = "Welcome to Python"
x = str.find("l")
print(x)
Output will give:
1
3. Find the word "e" from position 3 to position 10:
str = "Welcome to Python"
x = str.find("e", 3, 10)
print(x)
The Output will give:
6
4. Try to find a substring that does not appear:
str = "Welcome to Python"
x = str.find("a")
print(x)
The Output will give:
-1