- Read Tutorial
- Watch Guide Video
- Complete the Exercise
However, we also have a little bit more flexibility when it comes to working with strings and collections. So in this guide that is what we're going to walk through and we're going to be using what is called the in operator
. And the thing I love about this is it reads just like spoken language.
When we read the condition it is going to check to see if one element is inside or contained in another element. So I'm going to start off with strings and so I'm going to say sentence equals the quick brown fox jumped over the lazy dog.
sentence = 'The quick brown fox jumped over the lazy Dog'
And now if I create a new variable here called word and set this equal to quick what I can do is say. if word in sentence:
then I can print it out so I can say print('The word was found in the sentence')
. And so that will work. And let's add an else:
clause here and print('The word was not in the sentence')
. And so now if I run this you can see it says the word was found in the sentence.
Because quick is in the sentence and that's what we are looking for. Now, this seems to work pretty well, but now let's look for another word. I'm going to type in the word dog but notice even though we have a dog in this sentence our searching or our in operator is not case insensitive which means that if we look for dog spelled this way with the lowercase d. It is not going to find this Dog with an uppercase D.
So we can test this out by running it just like this. And as you can see the word was not found.
So there are a number of ways around this that you're going to find in Python. One of the common ones is to simply call the lower function on each one of these elements so if you want to perform a case-insensitive search you can just call whatever the variable is that contains a string and say I want you to take all of the values and change them to lowercase and the same thing with this word. And now if I run this you can see that it prints out that the word was found in the sentence so everything there is working properly.
Now I'm going to place all of this in the show notes.
Now let's see how the in operator works with collections of data. So if I come up here and create a list a list of nums so I can say something like nums = [1, 2, 3, 4]
. Now, what I can do is say if 3 in nums:
then we can print('The number was found')
and we're going to add our same else:
block and say the number was not found. And so now if we run this code you'll see that it says the number was found.
So this in operator is incredibly helpful because it checks not only for membership in strings so it not only tells you if one string is found inside of another string but it also can work with collections of data such as lists. So imagine that you perform a database query and you get back all these usernames and you want to check to see is one other person's username is it contained in that list of data?
You can run your code just like this, you do not have to go and create your own loop and check the entire list and see if it matches up. You can just ask python is this included and the proper way to say it is, is it a member of this collection and it works very nicely. And one of my favorite parts is because this does read just like you would say it where you check to see is this in nums and then it will return either true or false.
So that is how you can work with the in membership operator in Python.
Code
sentence = 'The quick brown fox jumped over the lazy Dog' word = 'dog' if word.lower() in sentence.lower(): print('The word is in the sentence') else: print('The word is not in the sentence') nums = [1, 2, 3, 4] if 3 in nums: print('The number was found') else: print('The number was not found')