Python練習 統計字串中的字元個數

2021-10-03 21:37:35 字數 849 閱讀 3299

統計字串中的字元個數

題目內容:

定義函式countchar()按字母表順序統計字串中所有出現的字母的個數(允許輸入大寫字元,並且計數時不區分大小寫)。形如:

def countchar(string):

… …return a list

ifname== 「main」:

string = input()

… …print(countchar(string))

輸入格式:

字串輸出格式:

列表輸入樣例:

hello, world!

輸出樣例:

[0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0]

def

countchar

(string)

: l =

list

('abcdefghijklmnopqrstuvwxyz'

) countlist =

string = string.lower(

)for i in

range

(len

(l)):)

)return countlist

if __name__ ==

"__main__"

: string =

input()

print

(countchar(string)

)

python 統計字串中的字元個數

題目內容 定義函式countchar 按字母表順序統計字串中所有出現的字母的個數 允許輸入大寫字元,並且計數時不區分大小寫 形如 defcountchar str return a list if name main str input print countchar str 輸入格式 字串 輸出格...

Python字串練習

python mystr hello world and dgjfkhgj title 返回乙個首字母大寫的字串 print mystr.title capitalize 返回第乙個首字母大寫的字串 print mystr.capitalize upper 返回全部大寫 print mystr.up...

python 字串練習

name gouguoq 移除name變數對應值的兩邊的空格,並輸出移除後的內容 print name.strip 判斷name變數對應的值是否以 go 開頭,並輸出結果 print name.startswith go 判斷name變數對應的值是否以 q 結尾,並輸出結果 print name.e...