String Search?

Fredrik Lundh effbot at telia.com
Tue Feb 29 12:31:53 EST 2000


Jason wrote:
> As a pre-note: Having just picked up Python,
> please excuse my ignorance.

umm.  is that really a valid excuse for not
having looked in the handbook? ;-)

> How exactly do I search for a given string within
> a string? i.e. how do I search for "foo" within
> the sentence "the foo falls at midnight"? The
> position of the string within the sentence isn't
> important, just the existance.

if string.find(mystring, "foo") >= 0:
    ...

or

string.index(mystring, "foo")
...

the latter raises a ValueError exception
if the substring isn't found.

for more info, see the "string" chapter in
the library reference.

hope this helps!

</F>





More information about the Python-list mailing list