函数练习题

1 写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数,并返回结果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
num1  =  input ( '请输入内容:' )
def  fun5(a):
     s1  =  0
     s2  =  0
     s3  =  0
     s4  =  0
     for  in  a:
         if  i.isdigit():   #统计数字
             s1  + =  1
         elif  i.isalpha():   #统计字母
             s2  + =  1
         elif  i.isspace():     #统计空格
             s3  + =  1
         else :                #统计其他
             s4  + =  1
     layout  =  '''
     数字个数为: {}
     字母个数为: {}
     空格个数为: {}
     其他个数为: {}
     '''
     return  layout. format (s1,s2,s3,s4)
print (fun5(num1))

猜你喜欢

转载自www.cnblogs.com/xyn123/p/8990268.html