Hunting Inside Words ๐
The word in asks: is this hiding inside that? It answers True or False,
just like your other comparisons.
print("berry" in "strawberry") # โ True
print("z" in "pizza") # โ True
print("cat" in "dog") # โ False
It works on whole sentences too, which makes it perfect for if:
message = "meet me at the treehouse"
if "treehouse" in message:
print("Secret location detected! ๐ณ")
โ ๏ธ Careful โ in is fussy about capital letters. "Berry" in "strawberry"
is False, because the big B doesn't match the little b.
Your mission
Build a snack detector!
- Make a variable
messageand store any sentence you like in it - Use
ifwithinto check whethermessagecontains the word "snack" โ print a reaction if it does - Add an
elsewith a different reaction - At the end, print one direct check on its own, like
print("berry" in "strawberry"), so you can see the True or False
Your code
Press โถ Run to see what your code does!