【パイソン】文字(20文字)の入力ラインを、それぞれ、その中に文字、スペース、数字および他の文字の数を数えることができます。

それぞれの文字の入力ライン(20文字以上)、その中に文字、スペース、数字および他の文字の数を数えることができます。
結果は次の通りです:
文字列を入力してください:45se R、d5d〜S58 *
手紙= 6、4 =スペース、数= 5、その他= 3

s=input('请输入一个字符串:\n')
letters=0
space=0
digit=0
others=0
for c in s:
    if c.isalpha():
        letters+=1
    elif c.isspace():
        space+=1
    elif c.isdigit():
        digit+=1
    else:
        others+=1
print('英文字母=%d 个,空格=%d 个,数字=%d 个,其他=%d 个'%(letters,space,digit,others))

ここに画像を挿入説明

公開された51元の記事 ウォンの賞賛229 ・は 10000 +を見て

おすすめ

転載: blog.csdn.net/famur/article/details/105197657