Python 3.7習題 文本字符統計

結合ASCII表統計英文字母,漢字,數字,符號數:
text=‘中國+china2017是-*/OK很難a也不難’
要求:
①用循環語句判斷統計
②打印統計結果

text='中國+china2017是-*/OK很難a也不難'
Eng=0
ch=0
numb=0
sym=0
i=0
while i<len(text):
    if ord(text[i])>127:
        ch+=1
    elif (ord(text[i])<123 and ord(text[i])>96):
        Eng+=1
    elif (ord(text[i])<90 and ord(text[i])>64):
        Eng+=1
    elif (ord(text[i])<58 and ord(text[i])>47):
        numb+=1
    else:
        sym+=1
    i+=1
print('英文字母數%d,漢字數%d,數字數%d,符號數%d'%(Eng,ch,numb,sym))

結果
在這裏插入圖片描述

發佈了16 篇原創文章 · 獲贊 0 · 訪問量 2668
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章