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

一、参考解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
s = input ( '请输入字符串:' )
dic = { 'letter' : 0 , 'integer' : 0 , 'space' : 0 , 'other' : 0 }
for i in s:
     if i > 'a' and i< 'z' or i> 'A' and i< 'Z' :
         dic[ 'letter' ] + = 1
     elif i in '0123456789' :
         dic[ 'integer' ] + = 1
     elif i = = ' ' :
         dic[ 'space' ] + = 1
     else :
         dic[ 'other' ] + = 1
         
print ( '统计字符串:' ,s)
print (dic)
print ( '------------显示结果2---------------'
for i in dic:
     print ( '%s=' % i,dic[i])
print ( '------------显示结果3---------------'
for key,value in dic.items():
     print ( '%s=' % key,value)

 

二、参考解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
tmpStr = input ( '请输入字符串:' )
alphaNum = 0
numbers = 0
spaceNum = 0
otherNum = 0
for i in tmpStr:
     if i.isalpha():
         alphaNum + = 1
     elif i.isnumeric():
         numbers + = 1
     elif i.isspace():
         spaceNum + = 1
     else :
         otherNum + = 1
print ( '字母=%d' % alphaNum)
print ( '数字=%d' % numbers)
print ( '空格=%d' % spaceNum)
print ( '其他=%d' % otherNum)

 

三、参考解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
InPut = input ( '请输入字符串:' )
letters  = [ ]
spaces = [ ]
digits   = [ ]
others = [ ]
for i in iter ( InPut ):
     if i.isalpha():
         letters.append(i)
     elif i.isspace():
         spaces.append(i)
     elif i.isdigit():
         digits.append(i)
     else :
         others.append(i)
print ( '''
字母: {}, 个数: {}
空格: {}, 个数: {}
数字: {}, 个数: {}
其他: {}, 个数: {}''' \
. format (letters, len (letters), spaces, len (spaces), digits, len (digits),others, len (others)))

 

四、参考解法:

使用正则表达式 re.findall()

复制代码
import re
s = input('请输入一串字符:') char=re.findall(r'[a-zA-Z]',s)#以列表类型返回全部能匹配的子串 num=re.findall(r'[0-9]',s) blank=re.findall(r' ',s) chi=re.findall(r'[\u4E00-\u9FFF]',s)#汉字的Unicode编码范围 other = len(s)-len(char)-len(num)-len(blank)-len(chi) print('字母',len(char),'\n数字',len(num),'\n空格',len(blank),'\n中文',len(chi),'\n其他',other)
复制代码

 五、参考解法:

使用正则表达式 re.match()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re
def splitFunc( ):
     tmpStr = input( '请输入字符串:' )
     charNum = 0
     digNum = 0
     spaceNum = 0
     otherNum = 0
     for i in range(len(tmpStr)):
         if re.match( '[a-zA-Z]' ,tmpStr[i]):
             charNum +=1
         elif re.match( '\d' ,tmpStr[i]):
             digNum +=1
         elif re.match( '\s' ,tmpStr[i]):
             spaceNum +=1
         else :
             otherNum +=1
     print( '字符:' ,charNum)
     print( '数字:' ,digNum)
     print( '空格:' ,spaceNum)
     print( '其他:' ,otherNum)
splitFunc()

转载于:https://www.cnblogs.com/Jintaonet/p/11150800.html

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值