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

 1 '''
 2 用Python方法辨别数据类型可以用python type()方法,
 3 那么想要查看一串字符中每一项的类型,
 4 并逐一输出要怎么来处理呢?
 5 
 6 Python练习题问题如下: 要求:
 7     输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
 8 
 9 '''
10 s = input('输入字符串:')
11 
12 others = 0
13 space = 0
14 digit = 0
15 alpha = 0
16 
17 for i in s:
18     if i.isdigit():
19         digit += 1
20     elif i.isalpha():
21         alpha += 1
22     elif i.isspace():
23         space += 1
24     else:
25         others += 1
26 print(others,space,digit,alpha)
为什么要坚持,想一想当初!
原文地址:https://www.cnblogs.com/JerryZao/p/8743364.html