python做統計字元 python統計字元個數

2021-10-19 20:43:42 字數 650 閱讀 4484

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做詞頻統計

假設有乙個本地的txt檔案,想對其進行詞頻統計,可以這樣寫 import time path c users zhangxiaomei desktop walden.txt with open path,r as text words text.read split print words forw...

python題目 字元統計

題目 輸入一行字元 分別統計出其英文本母 空格 數字和其他字元的個數 大寫字母 capital 0 小寫字母 lowercase 0 數字number 0 空格space 0 其他other 0 word list input 請輸入任意字元 for i in word if i.isalpha a...

python 統計字元頻次

統計字元頻次 輸入乙個字串,輸出其 現次數最多的字元及其出現的次數,要求使用字典。輸入格式 輸入任意長度字串。輸出格式 字串 現次數最多的字元及其次數。輸入樣例 在這裡給出一組輸入。例如 abcdsekjsiejdlsjdiejsl輸出樣例 在這裡給出相應的輸出。例如 s 4 python from...