Latest web development tutorials

Pythonは演習17と、

Pythonの100リー Pythonの100リー

タイトル:文字の行を入力し、それぞれ、統計する英文字、スペース、数字や他の文字の数。

解析プログラム:while文を使用して、条件が入力文字'\ n'がありません。

ソースコード:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import string
s = raw_input('input a string:\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 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others)

上の例の出力は、次のとおりです。

input a string:
w3big
char = 6,space = 0,digit = 0,others = 0

Pythonの100リー Pythonの100リー