Python统计字符个数

#Python 3.6(32-bit)

源代码如下:

str = input("please input a string from your keyboard:")                              #用户输入字符
eng=num=bla=oth=0  
for i in str:                                                                                                       #循环判断字符类型
    if (ord(i)>=97 and ord(i)<=122) or (ord(i)>=65 and ord(i)<=90):  
        eng=eng+1
    elif ord(i)>=48 and ord(i)<=57:  
        num=num+1
    elif ord(i)==32:  
        bla=bla+1  
    else:  
        oth=oth+1  
print("英文字母个数:{}个".format(eng))
print("数字个数:{}个".format(num))
print("空格个数:{}个".format(bla))
print("其他字符个数:{}个".format(oth))
 

你可能感兴趣的:(Python)