python中count函數(shù)的用法
count()函數(shù)
描述:統(tǒng)計字符串里某個字符出現(xiàn)的次數(shù),可以選擇字符串索引的起始位置和結束位置。
語法:str.count("char", start,end) 或 str.count("char")
返回值:整型
參數(shù)說明:
str —— 要統(tǒng)計的字符(可以是單字符,也可以是多字符)
star —— 索引字符串的起始位置,默認參數(shù)為0
end —— 索引字符串的結束位置,默認參數(shù)為字符串長度即len(str)
程序示例:
str = "i love python,i am learning python" print(str.count("i")) #star 和end 為默認參數(shù) print(str.count("i",2)) # star值為2,end值為默認參數(shù) print(str.count("i",2,5)) #star值為2,end值為5 print(str.count("am")) #多字符統(tǒng)計
程序運行結果:
3 2 0
推薦學習:Python教程