"str.contains(part)" or alternatives?
Delaney, Timothy
tdelaney at avaya.com
Wed Sep 11 23:52:46 EDT 2002
> From: Joseph A. Knapka [mailto:jknapka at earthlink.net]
>
> So in 2.3, can I do this and get the expected result?
>
> >>> "" in "abc"
> 1
Yes - this has been hotly debated and defined as true.
> >>> for subst in "abc":
> ... print subst
> ...
> <empty string>
> a
> ab
> abc
> b
> bc
> c
No. Iterating over a string is defined to return each single character in
turn. Remember:
for subst in "abc":
is equivalent to
for subst in iter("abc"):
and iter("abc") returns "a", "b", "c"
whilst
if subst in "abc":
is equivalent to
if "abc".__contains__(subst):
(falling back to if subst in iter("abc") if "abc" does not define
__contains__)
and str.__contains__ performs a substring match.
Tim Delaney
More information about the Python-list
mailing list