Python String Search function: count, find() index() differences, all, substring, word, last index, regex
In this post, we've collected some of the most frequently asked questions about how to find or search for strings in Python.
1. Finding a specific string location using the find() function
One of the most basic functions used to find a string in Python is the find()
function.
This function finds a given string in a string and returns its position.
If the string does not exist, it returns -1
.
s = "Hello, Python!"
print(s.find("Python")) # Output: 7
In the example above, you can find where the string "Python"
starts in "Hello, Python!"
.
In this case, 7
is returned, which means that the string "Python"
starts at index 7.
By default, the find()
function returns the index value of the first occurrence of the string you are looking for. For searching for multiple strings, see Section 4.
The function can also optionally take two more arguments: start index
and end index
, which can be used as follows, respectively.
s = "Hello, Python! Python is great."
print(s.find("Python")) # Output: 7
print(s.find("Python", 10)) # Output: 15
print(s.find("Python", 0, 15)) # Output: 7
In the example above, the second print
statement shows the behavior of the find()
function when it receives the starting index.
Because it starts at index 10
, it is equivalent to finding a position in the string s[10:]
, or "hon! Python is great."
.
Similarly, the third print
statement in the example is taken up to the end index and is equivalent to finding the position in a substring sliced by s[0:15]
.
If you want to find the last index of a particular string, you can use the rfind()
function, which works on the same principle.
This function searches from the end of the string, so it will find the last index where a particular string appears.
Note that there is also a corresponding rindex()
function to index()
function, which we'll discuss in Section 2.
1.1. Case-insensitive finding
As we saw in the Python String Comparison post, the __eq__()
method of the Python String class is case sensitive because it compares Unicode code values.
If you want to use the find()
function to find the case-insensitive position of a string, you must use the upper()
or lower()
methods.
In the following code, I've converted both the found string and the found string to lowercase for case-insensitive searching.
s = "Hello, Python!"
s.lower().find("python".lower()) # Output: 7
2. Difference between find() and index() functions
The find()
function and the index()
function are both methods used to find a specific substring within a string.
If the search is successful, you won't see any difference between the two methods.
However, if the search fails, the difference between the two methods becomes apparent.
If the string you are looking for does not exist in the search target, the find()
function will return the value -1
and throw no exceptions.
However, in the same situation, the index()
function will throw a ValueError
exception.
>>> s = "Hello, Python!"
>>> s.index("python")
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
Therefore, it is important to choose and use the appropriate one of the two methods depending on what you want to do when you cannot find a Python string.
3. Finding a specified number of strings
The method we use to find a specific number of strings in Python is count()
.
This method returns a count of how many times a given substring occurs in the target string.
Optionally, like the find()
and index()
methods, you can also specify an index to start and end the search.
The basic usage is
s = "Hello, Python! Python is great."
print(s.count("Python")) # Output: 2
print(s.count("Python", 2, 14)) # Output: 1
The first print
statement in the code above prints out the number of occurrences of the string "Python"
. In this case it returns 2
.
The second print
statement took the start index, the end index, and returned a different value based on that.
As you can see, the count()
method is useful for quickly determining the frequency of occurrence of a particular substring within a string, and can be used in a variety of situations where string analysis is required.
4. Finding all occurrences of a string
There are three main ways to find all occurrences of a string in Python.
The first is using the find()
function, and the other two are using the regular expression re
module.
4.1. Finding multiple strings using the find() function
As mentioned in Section 1, Python's find()
function only returns the index of the first occurrence of a substring.
Therefore, it must be used in conjunction with a loop to find the location of multiple strings.
There are many different ways to implement this, but here we'll look at the simplest implementation code.
s = "Hello, Python! Python is great."
start = 0
while start != -1:
start = s.find("Python", start)
if start != -1:
print(start)
start += 1
The above code gets multiple string locations using the start index that the find()
function takes as an argument and the value -1
that it returns if the search fails.
4.2. Getting multiple string locations with the re.finditer() function
The Python re
module is a library that helps you leverage powerful regular expressions with intuitive patterns and methods.
Among the functions within the re
module, the finditer()
function returns an iterable list of all matching objects that match a given pattern.
This can be useful when searching for multiple string positions because these matching objects contain position indices.
The position index is obtained using the .start()
method of the matching object.
Here's how to use it
import re
s = "Hello, Python! Python is great."
matches = re.finditer("Python", s)
indices = [match.start() for match in matches]
print(indices) # 출력: [7, 15]
You can use it in a variety of ways, such as with list comprehensions, as shown above.
5. Conclusion
In this article, we've covered four different ways to find strings in Python. With Python's strong support for string manipulation, we were able to search for substrings in a variety of ways. I hope you found this article useful in your real-world projects.
