Avoid Using .find() to Check for Substrings
This is where the first occurrence of the substring starts. But this has a name that is sometimes a little misleading for Python programmers because
.find() seems like a good way to find a string in Python, right? And I would argue that it is quite descriptively named.
I prefer to use
.index() because I have the feeling it’s a little more descriptive of what do both of these methods actually return because they return the index positions. Now there’s a difference between them, which is how they handle if they do not find the substring.
I’d rather have a
ValueError thrown that tells me this is not in there than have
-1. And the reason for that is that you’ll see people finding the
.find() string method and using it for finding a substring in a string, and then they start writing code, a conditional where they say if
text_lower.find(), let’s say
"substring not in string". Of course, your conditions would probably do something else in here. That’s where you write your code logic. But what I want to show you here is that if you opt for using
.find() in order to check for the substring in the string, then you come up with some kind of hard-to-read code constructs because in order to correctly interpret this if you’re reading code, you need to know that
-1 if it doesn’t find a substring, and that’s not entirely intuitive.
It works because we know that
-1 if it’s not found, we know
"treasure" is not in that string, so this whole expression is going to evaluate to
False, and because of that, you’re going to print out that the substring is not in the string.
So in this case you’re just saying is it in there, and Python returns
False, which means you can write your conditional statement like so. You can say
if—let me just copy that—
if "treasure" in text_lower:
I want to
print("substring not in string") just to remake the same structure as before. So in this case, the first line of this code is a lot easier to interpret if you use the
in operator than if you use
.find() and then compare to the
-1 return value. Okay, so this is just a little aside as to why you shouldn’t use
.find() or, for that matter,
.count() or any of these other string methods that you could massage in order to still basically perform a substrings check.
That said, all of these string methods still have their use cases. If you want to know how often is the substring in the string, you can use
str.count(). If you want to know where is the substring located, then you can use
str.index(), and you could also use
str.find() for that same purpose, and that just depends on whether you want to handle an error or whether you want to deal with the return value of
-1. And which of those two you wanted to use might really depend on your use case.
Become a Member to join the conversation.