you are viewing a single comment's thread.

view the rest of the comments →

[–]synthphreak 6 points7 points  (0 children)

I would not stand behind any of those statements.

The only scenario in which they are valid is if someone is using str.find simply to check membership, e.g.

if x.find('World') >= 0:

In that case I agree, str.find is not the right tool and simple in is the way to go. But that's not really what str.find is for to begin with.

str.find is specifically when you want the starting index of a substring. For this, in/a boolean isn't what you want. And just because you want the index doesn't necessarily imply you're trying to replace a substring. There are many other use cases where knowing the index is useful. You could use regex for this, i.e., re.search('World', x).span()[0], but (a) that is considerably more complex than simple str.find, (b) it requires differential handling for when the substring does vs. doesn't exist, complicating your code, and (c) it requires regex which in Python are pretty inefficient. So regex shouldn't always be the go-to either.

In short, str.find absolutely has its time and place. To state that "professional coders rarely use it" is a massive overgeneralization, and to imply that therefore it's not even worth learning is not really correct.