首頁  >  文章  >  後端開發  >  python中count函數的用法

python中count函數的用法

藏色散人
藏色散人原創
2020-01-04 10:59:3113198瀏覽

python中count函數的用法

python中count函數的用法

Python count()方法

描述

Python count() 方法用於統計字串裡某個字元出現的次數。可選參數為在字串搜尋的開始與結束位置。

count()方法語法:

str.count(sub, start= 0,end=len(string))

參數

sub -- 搜尋的子字串

start -- 字符串開始搜尋的位置。預設為第一個字元,第一個字元索引值為0。

end -- 字串中結束搜尋的位置。字元中第一個字元的索引為 0。預設為字串的最後一個位置。

傳回值

此方法傳回子字串在字串中出現的次數。

以下實例展示了count()方法的實例:

實例(Python 2.0 )

#!/usr/bin/python
 
str = "this is string example....wow!!!";
 
sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)

以上實例輸出結果如下:

str.count(sub, 4, 40) :  2
str.count(sub) :  1

推薦學習:Python影片教學

#

以上是python中count函數的用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn