Python中字串查詢方法

jiangjiane發表於2015-12-07

Python字串查詢有4個方法:find()、index()、rfind()、rindex()

1、find()方法

查詢子字串,若找到則返回從0開始的下標值,若找不到返回-1.

2、index()方法

在字串裡查詢子字串第一次出現的位置,類似於find()方法,不過比find()方法更好的是,如果查詢不到子字串,會丟擲異常,而不是返回-1.

3、rfind()和rindex()方法

用法和上面一樣,只是從字串的末尾開始查詢.


reference:

http://outofmemory.cn/code-snippet/6682/python-string-find-or-index

相關文章