Python provides a lot of string functions and functionalities. But finding string in a string or text is popular use case in the application developers. In this tutorial we will look different usage of
find function of string type.
We will use
find function and provide the string we are searching for. As we know
find function is provided by string types where we will search in. In this example we will search
pof in the string variable named
mytext="This is one of the poftut examples" mytext.find("pof") #19
As we can see that the start index is the string is returned. If the given string is not founded in the text
mytext it will be
Find From Given Index
While searching given string in a text it may not convenient start searching from begging of the text.
find function provides the ability to specify where to start. We will provide the start index of the search operation and search to the end of the text.
In this example we will start from index
10 which is tenth character of the text.
mytext="This is one of the poftut examples" mytext.find("pof",10) #19
Set Search End Index
Another search limiting feature is setting the end of the search index. So the
find function will search up to given index. In this example we should provide both start and end index. If we want to start searching from begging we can set it as
. We will search up to
mytext="This is one of the poftut examples" mytext.find("pof",0, 10) #-1