How to find "in" in the documentation
marduk at letterboxes.org
Fri Mar 13 22:26:00 CET 2009
On Fri, 2009-03-13 at 21:01 +0000, tinnews at isbd.co.uk wrote:
> I've had this trouble before, how do I find the details of how "in"
> works in the documentation. E.g. the details of:-
> if string in bigstring:
> It gets a mention in the "if" section but not a lot.
The operators in and not in test for collection membership. x in
s evaluates to true if x is a member of the collection s, and
false otherwise. x not in s returns the negation of x in s. The
collection membership test has traditionally been bound to
sequences; an object is a member of a collection if the
collection is a sequence and contains an element equal to that
object. However, it make sense for many other object types to
support membership tests without being a sequence. In
particular, dictionaries (for keys) and sets support membership
For the list and tuple types, x in y is true if and only if
there exists an index i such that x == y[i] is true.
For the Unicode and string types, x in y is true if and only if
x is a substring of y. An equivalent test is y.find(x) != -1.
Note, x and y need not be the same type; consequently, u'ab' in
'abc' will return True. Empty strings are always considered to
be a substring of any other string, so "" in "abc" will return
Changed in version 2.3: Previously, x was required to be a
string of length 1.
For user-defined classes which define the __contains__() method,
x in y is true if and only if y.__contains__(x) is true.
For user-defined classes which do not define __contains__() and
do define __getitem__(), x in y is true if and only if there is
a non-negative integer index i such that x == y[i], and all
lower integer indices do not raise IndexError exception. (If any
other exception is raised, it is as if in raised that
More information about the Python-list