Another Wart! string.find() [ was: namespace issue? ]

Jeff Shannon jeff at ccvcorp.com
Fri Jun 22 12:56:55 EDT 2001


Michael Powe wrote:

> >>>>> "Andrew" == Andrew Dalke <dalke at acm.org> writes:
>
>     Andrew>
>
>     >>>> print string.find.__doc__
>     Andrew> find(s, sub [,start [,end]]) -> in
>     ....

>     Andrew> Return -1 on failure.
>     >>>> print string.index.__doc__
>     Andrew> index(s, sub [,start [,end]]) -> int
>     .....
>     Andrew>  Raise ValueError if not found.
>
> Pardon the dumb question, but can you test this in a conditional, as
> in
>
> if not string.index(s,sub) : /* and so forth and so on */
>
> mp

No, because you're not catching the exception.  You could, however, do

try:
    string.index(s, sub)
except ValueError:
    pass
else:
    DoSomething(s)

instead.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list