×


find() Function in Python - Explained with Examples



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.


More about find() Function in Python

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:

  • value: substring that you need to find.
  • start: starting value. Default is 0.
  • end: ending value. Default is the end of the string.


Examples of using find() Function

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


[Need help in fixing Python function issues ? We can help you. ]


Conclusion


More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com