This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Start argument for str.rfind used incorrectly
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.1, Python 2.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, dtorp, r.david.murray
Priority: normal Keywords:

Created on 2010-04-27 22:20 by dtorp, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg104375 - (view) Author: David Albert Torpey (dtorp) Date: 2010-04-27 22:20
The purpose of the start argument in str.find() and str.rfind() is to allow for repeated searches.  

>>> def find_third_occurrence(s, value):
...	p = s.find(value)
...	p = s.find(value, p+1)
...	return s.find(value, p+1)
>>> find_third_occurrence('scientific american', 'c')

The rfind() method is meant for searching from the right, but its start argument is used internally as if it were searching from the left.  This bug makes it useless for repeated searches from the right.

>>> 'scientific american'.rfind('c')
>>> 'scientific american'.rfind('c', 15)
>>> 'scientific american'.rfind('c', 14)

Having found the first 'c' from the right at position 16, there is no way to tell it to search further from the right and find the second 'c' at position 9.
msg104378 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-04-27 22:53
This is in intentional for consistency with find().
msg104388 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-04-28 00:28
I thought Benjamin's answer was crazy until I looked at the help for find/rfind.  The optional 'start, end' arguments are interpreted like slice notation, so it does make sense to have the interpretation be the consistent between find and rfind.
Date User Action Args
2022-04-11 14:57:00adminsetgithub: 52797
2010-04-28 00:28:18r.david.murraysetnosy: + r.david.murray

messages: + msg104388
stage: resolved
2010-04-27 22:53:25benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg104378

resolution: not a bug
2010-04-27 22:20:20dtorpcreate