Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Find and Seek

Give Feedback

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

Become a Member to join the conversation.