Python String Find Tutorial with Examples – POFTUT

Python String Find Tutorial with Examples


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.

Just Find

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 .

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 -1 returned.

mytext.find("pofff") 
#-1

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
Find From Given Index
Find From Given Index

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 0 . We will search up to 10 .

mytext="This is one of the poftut examples" 
mytext.find("pof",0, 10)                   
#-1
Set Search End Index
Set Search End Index

LEARN MORE  How To Get Current Date Time with Now() Function In Python?

Leave a Comment