faqts : Computers : Programming : Languages : Python : Modules : string

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 9 people (89%) answered Yes
Recently 6 of 7 people (86%) answered Yes

Entry

How can I determine if a string is contained in a bigger string?
How can I find the position of a substring in a string?

Mar 1st, 2000 19:24
Nathan Wallace, Fredrik Lundh


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:

  http://www.python.org/doc/current/lib/module-string.html