python练习程序(c100经典例17)

时间:2021-03-01 04:27:32

题目:

输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

def foo(a):
    l=len(a);
    letters=0;
    space=0;
    digit=0;
    others=0;
    for i in range(0,l):
        num=ord(a[i])
        if num>=ord('a') and num<=ord('z') or num>=ord('A') and num<=ord('Z'):
            letters=letters+1;
        '):
            digit=digit+1;
        elif num==ord(' '):
            space=space+1;
        else:
            others=others+1;
    print letters,space,digit,others

foo('sadfsa34564   ,.,.')