liveBook · Manning

Membership operator

As explained in Get Programming: Learn to code with Python

A membership operator is a language construct that tests whether a specified value or property is present in a supported collection, range, or object.

A membership test asks whether a value or property is present in a collection. For strings, a related way to check for a substring is `find()`: it returns the starting index when the substring is present and `-1` when it is absent. For example, `"some_string".find("ing")` returns `8`, while `"who".find("you")` returns `-1`. section 8.1

Searching a string

With `who = "me myself and I"`, `who.find("and")` returns `10`; `who.find("you")` returns `-1`. Use `rfind()` to locate a matching substring nearest the end instead. section 8.1

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains find() and rfind() results with examples, but does not detail membership-operator syntax or behavior.
Examples
Shows searching for "and" and "you" in "me myself and I" and finding "ing" in "some_string".
What it assumes
Assumes familiarity with retrieving string substrings using indices; no broader prerequisites are specified.
What you can do after
Evaluate substring searches with find() and interpret the returned index or -1.
How it connects
Connects substring searching with strings, indices, find(), and rfind().

Also explained in

Compare how 8 books explain membership operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.