Python
Python Functions Examples: Call, Indentation, Arguments & Return Values
What is Function in Python? A Function in Python is a piece of code which runs when it is...
Python String find() is a function available in Python library to find the index of the first occurrence of a substring from the given string. The string find() function will return -1 instead of throwing an exception, if the specified substring is not present in the given string.
In this Python string find() method tutorial, you will learn:
The basic syntax of Python find() function is as follows:
string.find(substring,start,end)
The parameters passed to Python find() method are substring i.e the string you want to search for, start, and end. The start value is 0 by default, and the end value is the length of the string.
In this example, we will use the method find() in Python with default values.
The find() method will search for the substring and give the position of the very first occurrence of the substring. Now, if the substring is present multiple times in the given string, still it will return you the index or position of the first one.
Example:
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials"))
Output:
The position of Tutorials is at: 12
You can search the substring in the given string and specify the start position, from where the search will begin. The start parameter can be used for the same.
The example will specify the start position as 15, and the find() in Python method will begin the search from position 15. Here, the end position will be the length of the string and will search till the end of the string from 15 positions onwards.
Example:
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials", 20))
Output:
The position of Tutorials is at 48
Using the start and end parameter, we will try to limit the search, instead of searching the entire string.
Example:
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials", 5, 30))
Output:
The position of Tutorials is at 12
We know that find() helps us to find the index of the first occurrence of substring. It returns -1 if the substring is not present in the given string. The example below shows the index when the string is present and -1 when we don't find the substring we are searching for.
Example:
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Best site is at:", mystring.find("Best site", 5, 40))
print("The position of gtupapers is at:", mystring.find("gtupapers", 20))
Output:
The position of Best site is at: 27 The position of gtupapers is at: -1
The Python function rfind() is similar to find() function with the only difference is that rfind() gives the highest index for the substring given and find() gives the lowest i.e the very first index. Both rfind() and find() will return -1 if the substring is not present.
In the example below, we have a string "Meet gtupapers Tutorials Site. Best site for Python Tutorials!" and will try to find the position of substring Tutorials using find() and rfind(). The occurrence of Tutorials in the string is twice.
Here is an example where both find() and rfind() are used.
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("Tutorials"))
print("The position of Tutorials using rfind() : ", mystring.rfind("Tutorials"))
Output:
The position of Tutorials using find() : 12 The position of Tutorials using rfind() : 48
The output shows that find() gives the index of the very first Tutorials substring that it gets, and rfind() gives the last index of substring Tutorials.
The Python string index() is function that will give you the position of the substring given just like find(). The only difference between the two is, index() will throw an exception if the substring is not present in the string and find() will return -1.
Here is a working example that shows the behaviour of index() and find().
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("Tutorials"))
print("The position of Tutorials using index() : ", mystring.index("Tutorials"))
Output:
The position of Tutorials using find() : 12 The position of Tutorials using index() : 12
We are getting same position for both find() and index(). Let us see an example when the substring given is not present in the string.
mystring = "Meet gtupapers Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("test"))
print("The position of Tutorials using index() : ", mystring.index("test"))
Output:
The position of Tutorials using find() : -1
Traceback (most recent call last):
File "task1.py", line 3, in <module>
print("The position of Tutorials using index() : ", mystring.index("test"))
ValueError: substring not found
In the above example, we are trying to find the position of substring "test". The substring is not present in the given string, and hence using find(), we get the position as -1, but for index(), it throws an error as shown above.
To find the total number of times the substring has occurred in the given string we will make use of find() function in Python. Will loop through the string using for-loop from 0 till the end of the string. Will make use of startIndex parameter for find().
Variables startIndex and count will be initialized to 0. Inside for –loop will check if the substring is present inside the string given using find() and startIndex as 0.
The value returned from find() if not -1, will update the startIndex to the index where the string is found and also increment the count value.
Here is the working example:
my_string = "test string test, test string testing, test string test string"
startIndex = 0
count = 0
for i in range(len(my_string)):
k = my_string.find('test', startIndex)
if(k != -1):
startIndex = k+1
count += 1
k = 0
print("The total count of substring test is: ", count )
Output:
The total count of substring test is: 6
What is Function in Python? A Function in Python is a piece of code which runs when it is...
Python count The count() is a built-in function in Python. It will return the total count of a...
Python List data-type helps you to store items of different data types in an ordered sequence. The...
Python code editors are designed for the developers to code and debug program easily. Using these...
What is Python Certification? Python certification training courses help you to master the...
What is XML? XML stands for eXtensible Markup Language. It was designed to store and transport...