Using string methods and regexes in Python

5 Ways to Find the Index of a Substring in Strings in Python
str.find()
str.rfind()
str.index()
str.rindex()
re.search()
str.find()
str.find()
returns the lowest index in the string where the substring sub
is found within the slice s[start:end]
. It returns -1
if the sub is not found.
start
and end
are optional arguments.
str.find(sub,start,end)
Example 1. Using str.find() method

The string is banana
.
The substring is an
.
The substring occurs two times in the string.
str.find(“an”)
returns the lowest index of the substring an
.
s1="banana" print (s1.find("an")) #Output:1
Example 2. Using str.find() method with the start parameter mentioned
The substring is an
.
The start parameter is 2
. It will start searching the substring an
from index 2.
s1="banana" print (s1.find("an",2)) #Output:3
Example 3. If the substring is not found, it will return -1
The substring is ba
.
The start parameter is 1
and the stop parameter is 5
. It will start searching the substring from index 1 to index 5 (excluded).
Since the substring is not found in the string within the given index, it returns -1
.
s1="banana" print (s1.find("ba",1,5)) #Output:-1
2. str.rfind()
str.rfind()
returns the highest index in the string where the substring sub
is found within the slice s[start:end]
. It returns -1
if the sub is not found.
start
and end
are optional arguments.
str.rfind(sub,start,end)
Example 1. Using str.rfind() method

The string is banana
.
The substring is an
.
The substring occurs two times in the string.
str.find(“an”)
returns the highest index of the substring an
.
s1="banana" print (s1.rfind("an")) #Output:3
Example 2. Using str.rfind() method with the start and end parameters mentioned
The substring is an
.
The start
and end
parameters are 1
and 4
, respectively. It will start searching the substring from index 1 and index 4 (excluded).
s1="banana" print (s1.rfind("an",1,4)) #Output:1
Example 3. If the substring is not found, it will return -1
The substring is no
.
Since the substring is not found in the string, it returns -1
.
s1="banana" print (s1.rfind("no")) #Output:-1
3. str.index()
Similarly to find()
, str.index()
returns the lowest index of the substring found in the string. It raises a ValueError
when the substring is not found.
Example 1. Using str.index() method
s1="banana" print (s1.index("an")) #Output:1
Example 2. Using str.index() method with the start and end parameters given
s1="banana" print (s1.index("an",2,6)) #Output:3
Example 3. If the substring is not found, it raises a ValueError
s1="banana" print (s1.index("no")) #Output:ValueError: substring not found
4. str.rindex()
Similarly to find()
, str.rindex()
returns the highest index of the substring found in the string. It raises a ValueError
when the substring is not found.
Example 1. Using str.rindex() method
s1="banana" print (s1.rindex("an")) #Output:3
Example 2. Using str.index() method with the start and end parameters given
s1="banana" print (s1.rindex("an",0,4)) #Output:1
Example 3. If the substring is not found, it raises a ValueError
s1="banana" print (s1.rindex("no")) #Output:ValueError: substring not found
5. re.search()
re.search(pattern, string, flags=0)
“Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return
None
if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.” — Python’s official documentation
re.search
(pattern, string): We have to mention thepattern
to be searched in thestring
.- The return type matches the object that contains the starting and ending index of that pattern (substring).
- We can find the
start
andend
indices from the match object usingmatch.start()
andmatch.end()
.
Match.start([group])
Match.end([group])
“Return the indices of the start and end of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return
-1
if group exists but did not contribute to the match.” — Python’s documentation
- We can get the
start
andend
indices in tuple format usingmatch.span()
.
Match.span([group])
“For a match m, return the 2-tuple
(m.start(group), m.end(group))
. Note that if group did not contribute to the match, this is(-1, -1)
. group defaults to zero, the entire match.” — Python’s documentation
Example 1. Using re.search()
https://gist.github.com/BetterProgramming/195e178ce90df0db15f8bd6acfb5236e#file-re-search-py
Example 2. If a substring is not found in the string, it returns None
import re string = 'banana' pattern = 'no' match=(re.search(pattern, string)) #Returns match object print (match)#Output: None
Conclusion
- Python 3.8.1 is used.
str.find()
,str.rfind()
— Returns-1
when a substring is not found.str.index()
,str.rindex()
— Raises aValueError
when a substring is not found.re.search()
— ReturnsNone
when a substring is not found.str.find()
,str,index()
— Returns the lowest index of the substring.str.rfind()
,str.rindex()
— Returns the highest index of the substring.re.search()
— Returns the match object that contains the starting and ending indices of the substring.
My other blogs related to string methods
split() vs. partition() in Python Strings
Remove Whitespaces from Strings in Python
5 Different Ways to Remove Specific Characters From a String in Python
Different Ways to Replace Occurences of a Substring in Python Strings