In today’s practice question, we are going to write a function that gives us all the occurrences of a certain substring in a string.
Here are some topics we’ll cover in this post:
- How to use the built-in
find()
function - How to return a list from a Python function
- How to use the
while True
loop
Table of Contents
Key Concepts
The Python find() function
In order to work on today’s practice question, we need to know how to use the built-in find()
function in Python.
This function searches for a substring in a given string and returns the position of the first occurrence of the substring. If the substring is not found, it returns -1.
The Python find()
function takes three arguments – the string to find, the position to start searching, and the position to stop searching. Its syntax is as follows:
find(substring_to_find, start, end)
start
represents the position to start searching.
end
, on the other hand, represents the first position that is not part of the search.
Positions start from 0. For instance, for the string 'Python'
, ‘P’ is at position 0, ‘y’ is at position 1 and so on.
Positions can also be negative. If it is negative, we start counting from the back of the string. Negative positions start from -1. For the string 'Python'
, ‘n’ is at -1, ‘o’ is at -2 etc.
Suppose start = 2
and end = 5
, the find()
function searches from position 2 to 4 (not 5). Position 5 is the first position that is not part of the search.
Both start
and end
are optional. If start
is omitted, the function starts searching from position 0. If end
is omitted, the function searches from start
to the end of the string.
Let’s look at some examples:
msg = 'Good morning, this is a beautiful morning'
print(msg.find('morning'))
print(msg.find('morning', 6))
print(msg.find('morning', 6, 20))
print(msg.find('morning', -10))
Here, we first declare a variable called msg
with the value 'Good morning, this is a beautiful morning'
.
Next, we use msg
to call the find()
function, passing the substring 'morning'
as argument to the function.
On line 3, we want the function to search for the substring from the start of msg
to the end.
On line 4, we want the function to search from position 6 to the end of the string. In other words, we want the function to search the string as shown below (where positions replaced by asterisks are to be ignored):
******orning, this is a beautiful morning
On line 5, we want the function to search the string from position 6 to 19. In other words, we want the function to search the string as shown below:
******orning, this i*********************
Last but not least, on line 6, we want the function to search the string from position -10 to the end of the string. In other words, we want the function to search the string as shown below:
*******************************ul morning
If you run the code above, you’ll get the following output:
5
34
-1
34
Study the outputs carefully and make sure you understand how the find()
function works before proceeding.
For line 2 in the output above, we get 34 as the find()
function is searching for the substring 'morning'
from position 6 to the end of the string.
As the first occurrence of 'morning'
in msg
starts at position 5, the find()
function is unable to find this first occurrence. Instead, it gives us the position of the second occurrence.
Got it? We’ll be using this concept to complete the practice question today.
Let’s proceed to the question now.
Practice Question
The task for today’s practice is to write a function called findAll()
that takes in two arguments – the substring to find and the string used for searching.
This function should return a list of the positions of all occurrences of the substring.
Expected Results
To test your function, you can run the code below:
print(findAll('Ho', 'Ho Ho Ho HolidaysHolidaysHo'))
Here, we want the findAll()
function to search for the substring 'Ho'
in the string 'Ho Ho Ho HolidaysHolidaysHo'
.
If you run the statement above, you should get the following output:
[0, 3, 6, 9, 17, 25]
Additional Notes
How to return a list from a Python function
Returning a list from a Python function is straightforward, you simply use the return
keyword. Here’s an example:
def listDemo():
position = []
position.append(5)
position.append(7)
return position
print(listDemo())
Here, we define a function called listDemo()
on line 1.
Inside the function, we declare an empty list called position
. Next, we add two numbers (5 and 7) to position
using the append()
method.
Finally, we use the return
keyword to return the list.
Outside the function, we call the listDemo()
function and use the print()
function to print its result.
If you run the code above, you’ll get the following output:
[5, 7]
Suggested Solution
Here’s the suggested solution for today’s practice question:
def findAll(subString, myString):
position = []
result = 0
while True:
result = myString.find(subString, result)
if result == -1:
break
else:
position.append(result)
result += 1
return position
On line 1, we declare a function called findAll()
with two parameters – subString
and myString
.
Inside the function, we declare and initialise two variables, position
and result
.
Next, we use a while True
loop to repeatedly call the built-in find()
function.
A while True
loop runs infinitely until we use a break
statement to break out of the loop. In our program above, the while True
loop keeps running until the if
condition on line 7 evaluates to True
. In other words, it keeps running until the find()
function returns -1 on line 6.
When that happens, we exit the while
loop using the break
statement.
On the other hand, if the find()
function does not return -1 on line 6, the else
block is executed. Inside the else
block, we add the result returned to position
and increment the value of result
by 1.
The purpose of incrementing result
by 1 is to update the starting position used by the find()
function. This ensures that the find()
function will ignore the previous occurrence of subString
and search for the next occurrence when the loop runs again.
Once the find()
function finds all occurrences of subString
and returns -1, we break out of the while True
loop and return the result on line 12.
With that, the function is complete.