In Python, the in
keyword is used to check whether a value is present in a sequence, such as a string, list, tuple, or set.
The syntax for using the in
keyword is as follows:
value in sequence
Here, value
is the item you want to check for, and sequence
is the sequence you want to check in. The result of the in
operator is a boolean value (True
or False
) depending on whether the value is present in the sequence.
For example, let’s say we have a list of fruits:
fruits = ["apple", "banana", "orange", "kiwi"]
To check if “banana” is present in the list, we can use the in
keyword:
if "banana" in fruits: print("Yes, banana is in the list!") else: print("No, banana is not in the list.")
This will output:
Yes, banana is in the list!
Similarly, we can use the in
keyword to check if a substring is present in a string, like this:
text = "The quick brown fox jumps over the lazy dog" if "fox" in text: print("Yes, 'fox' is in the text.") else: print("No, 'fox' is not in the text.")
This will output:
Yes, 'fox' is in the text.