Student Handbook

Student Handbook

Open in ChatGPT
Ask ChatGPT about this page
Open in Claude
Ask Claude about this page

in

Checks whether a piece of text, or an item, is inside another.

Synopsis

part in text
item in list
item not in list

Description

in is an operator, not a function. It gives True or False.

Return value

A bool.

Example

print("ell" in "hello")
print("z" in "hello")
print("yes" in ["y", "yes"])
print(3 not in [1, 2])

It prints:

True
False
True
True

Common mistake

answer = "no"
if answer == "y" or "yes":
    print("Agreed.")

It prints:

Agreed.

Each side of or is checked on its own, and "yes" alone counts as true, so this always agrees. Write if answer in ["y", "yes"]:.

See also

str.startswith, str.lower

in in the Python documentation

Last updated 2 weeks ago