Menu Close

Python String find() Method

python string find() method

In this article, we are going to learn about the python string find() method. string find function is a string function that is only used with string to find the first occurrence of a specified value.
In the previous tutorial, we have seen the python string expandtabs() method.

Python String find() Method:

The Python string find() method is a string method which only used with the string data type.
The find() method which is used to find the substring in the whole string and return the number of index of the first match otherwise return -1 for not match.

Syntax:

The syntax of Python string find() method is:

string.find(substring, start, stop)

Parameter:

The python string find function accept three parameters:-

substring – Required
start – ( Optional ) Position where the search start.
stop – ( Optional ) Position where the search end.

Let’s see an example for using Python find() string method.

Python String find() Method Example:

Here we will take various example to understand string find() method.

Example 1:

str = "Welcome to the programming funda."
print("Old string:- ", str)

str2 = str.find('the')
print(str2)

Output

Old string:-  Welcome to the programming funda.
11

Example 2:

str = "Programming Funda"
print("Old string:- ", str)

str2 = str.find('Fu', 12, 17)
print(str2)

Output

Old string:-  Programming Funda
12

Example 3:

str = "Python"
print("Old string:- ", str)

str2 = str.find('Pro')
print(str2)

Output will be:- -1

Conclusion:

In this tutorial, you have learned about Python string find() method to find any substring from string. This is the best method, If you want to find any substring from string in specific range.

I hope this string method tutorial will help you. If you like this article, please share with your friends who want to learn Python program from scratch to advanced.

For More Information:- Click Here

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply