Confirm the Presence of a Substring
00:00
All right, let’s start by confirming the presence of a substring. And in Python, the most straightforward way of doing this is by using the in
operator.
00:11
And here’s the syntax of how you can check for a substring in the string. You first type the substring, then you type the in
operator, and then you type the string where you want to check for the substring.
00:27
To show that with a concrete example, here you go. So you want to check whether "toes"
is in "tomatoes"
. In this case, if you would punch that into a Python REPL, then you’ll see that the in
operator returns a Boolean value, and that means it’s either going to be True
or False
. And in this case, because the substring "toes"
appears inside of the string "tomatoes"
, you’ll get True
back. Okay, let’s go ahead and try that out.
00:58
I’m here in an IPython console, which just gives a little bit more syntax highlighting so that it’s easier for you to follow along, but the functionality is going to be the same if you use just the normal Python REPL. All right, so we want to say "toes" in "tomatoes"
.
01:17
As you can see, Python returns True
because the substring is found inside of the string. Now, if you had something else, the opposite of the substring is found would be that the substring isn’t found. And in that case, Python would return False
. So let’s try that out with
01:38
"fingers" in "tomatoes"
. And here you get a False
. But stay here. Don’t head off to make ketchup the old-fashioned way. Let’s look at a use case that may be a bit more interesting, where you’re checking whether a certain word appears in a longer text.
01:54 So I have a text here. I will assign it to a string.
02:02 This is a string made up of three sentences. The word secret appears multiple times. Note, however, that the capitalization of the word secret is different each time.
02:11
One time uppercase, one time lowercase, and one time capitalized. And the word secretly also appears in the text. You’ll learn more about why these slight differences matter. First, let’s check for the lowercase string "secret"
by using the in
operator, just as before. And because the substring "secret"
appears in the text, you’ll get a response of True
.
02:37 Most commonly, you might want to use this type of check if you want to take a decision in your code, which means that you might want to use a conditional statement.
02:48
So you could say if "secret" in text:
02:55
print()
something. Oh yeah, one of those nice smiley faces can stay, and then I’ll say,
03:09
So now, if we execute this conditional statement, then Python is going to perform the same check it did before. It’s going to check whether the substring "secret"
is in the variable text
that I defined before, so this is this long string.
03:25
And if it’s in there, then Python’s going to print out "It's there!"
and one of those celebrating emoji faces. And as you’ve seen before, the word "secret"
appears in that text
string, and therefore you get this type of output here. Now, you would probably have some sort of code logic in there that maybe executes something depending on whether or not the substring is inside of the larger string.
03:53
This is the most common way you’re going to use a substring check in Python. The takeaway is use the in
operator, and it’s going to return a Boolean, so either True
or False
, depending on whether or not the substring is inside of the string.
04:09
But one thing to remember with this check is that it is case sensitive, so Python really just looks for a lowercase "secret"
here. And in the next lesson, you’re going to take a look how you can generalize this a little bit to maybe also catch other capitalizations of a substring that you might be looking for in a larger string.
Become a Member to join the conversation.