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()
:
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?