Python活力练习Day11

Day11:输入一行字符,分别统计出其中的英文字母,空格,数字以及其他字符的个数

# 还有一个直接可以判断字符或者数字的方法:s.isalnum()

 1 def fun():
 2     '''
 3     s:输入的字符串
 4     alp:字母长度
 5     num:数字长度
 6     spa:空白字符长度(包括'
','	')
 7     others:其他
 8     '''
 9     s = input("please enter a string:	")
10     alp,num,spa,others = 0,0,0,0
11     for i in s:
12         if i.isalpha():
13             alp +=1
14         elif i.isdigit():
15             num += 1
16         elif i.isspace():
17             spa += 1
18         else:
19             others += 1
20     print("length = %d,char = %d,digit = %d,space = %d,others = %d"%(len(s),alp,num,spa,others))
21 
22 if __name__ == '__main__':
23     fun()

输出结果:

原文地址:https://www.cnblogs.com/xiaodangdang/p/12108680.html