Python 的字符串類有個很好用的函數,可很方便的用於與查找Python字符串類型對象子串相關的操作,具體的API如下:
| find(...) | S.find(sub [,start [,end]]) -> int | | Return the lowest index in S where substring sub is found, | such that sub is contained within s[start:end]. Optional | arguments start and end are interpreted as in slice notation. | | Return -1 on failure
假定S為string類型的對象,調用find函數,將子串或子串的子串(也就是子串從start到end的切片)作為參數傳入。
返回值為int類型,若S包含有傳入參數sub,則返回子串(substring)在父串(S)中的索引,若不包含作為參數傳入的子串,則返回-1代表查找索引失敗。