题目:输入一行字符,分别统计出其中英文字母、…
2018-06-27 09:46:07来源:博客园 阅读 ()
一、参考解法:
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()
二、参考解法:
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)
三、参考解法:
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)
四、参考解法:
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)))
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Python 用(无脑 and 有脑)方式解决小练习 2019-07-24
- python列表生成式、键盘输入及类型转换、字符串翻转、字母大 2019-07-24
- python的输入和输出 2019-07-24
- 输入某年某月某日,判断这一天是这一年的第几天 2019-07-24
- 剑指offer--day01 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
