Python练习:输入字符统计空格字母数字和其他字符(条件循环 输入输出 格式化输出 计算方式)
发布时间
阅读量:
阅读量
#Python 练习实例17
#题目:输入一段文字内容,分别计算其中包含的英文字母数量、空格数量、数字字符数量以及其余字符的数量。
#程序分析:可通过 while 或 for 循环结构实现,循环条件设定为输入的字符不等于 ‘\n’。
import string
n=input("请输入一串字符串:\n")
letters=0
space=0
digit=0
others=0
for c in s:
if c.isalpha():#判断字母
letters=letters+1
elif c.isspace():#判断空格
space=space+1
elif c.isdigit():#判断数值
digit=digit+1
else:#判断其他
others+=1
print("char=%d,space=%d,digit=%d,others=%d"%(letters,space,digit,others))#格式化输出
全部评论 (0)
还没有任何评论哟~
