In this lesson, you’ll explore string methods that provide various means of searching the target string for a specified substring.
Each method in this group supports optional <start>
and <end>
arguments. These are interpreted as for string slicing: the action of the method is restricted to the portion of the target string starting at character position <start>
and proceeding up to but not including character position <end>
. If <start>
is specified but <end>
is not, then the method applies to the portion of the target string from <start>
through the end of the string.
These are find and seek methods:
str.count(<sub>[, <start>[, <end>]])
str.endswith(<sub>[, <start>[, <end>]])
str.startswith(<sub>[, <start>[, <end>]])
str.find(<sub>[, <start>[, <end>]])
str.rfind(<sub>[, <start>[, <end>]])
str.index(<sub>[, <start>[, <end>]])
str.rindex(<sub>[, <start>[, <end>]])
Here’s how to use str.count()
:
>>> s = 'spam ham clam jam'
>>> s.count('am')
4
>>> s.count('am', 0, 9)
2
Here’s how to use str.endswith()
:
>>> s = 'baconsausage'
>>> s.endswith('age')
True
>>> s.endwith('ages')
False
>>> s.endswith('ons', 0, 9)
False
>>> s.endswith('ons', 0, 6)
True
Here’s how to use str.startswith()
:
>>> s = 'baconsausage'
>>> s.startswith('bac')
True
>>> s.startswith('saus')
False
>>> s.startswith('saus', 5)
True
>>> s.startswith('saus', 5, 7)
False
Here’s how to use str.find()
:
>>> s = 'spam bacon egg sausage'
>>> s.find('egg')
11
>>> s.find('spam')
0
>>> s.find('lobster')
-1
>>> s.find('egg', 9, 20)
11
>>> s.find('egg', 9)
11
>>> s.find('egg', 5, 10)
-1
Here’s how to use str.rfind()
:
>>> s = 'spam bacon spam spam egg spam'
>>> s.find('spam')
0
>>> s.rfind('spam')
25
>>> s.rfind('spam', 8, 15)
11
>>> s.rfind('spam', 8, 10)
-1
Here’s how to use str.index()
:
>>> s = 'spam bacon spam spam egg spam'
>>> s.index('spam')
0
>>> s.index('spm')
Traceback (most recent call last):
File "<input>", line 1, in <module>
s.index('spm')
ValueError: substring not found
Here’s how to use str.rindex()
:
>>> s = 'spam bacon spam spam egg spam'
>>> s.rindex('spam')
25
>>> s.rindex('spm')
Traceback (most recent call last):
File "<input>", line 1, in <module>
s.rindex('spm')
ValueError: substring not found
davidnierman93 on Dec. 14, 2020
When would you use index and rindex instead of find and rfind?
In other words, can I see a use case where returning an error is preferred over returning -1? and vice versa?