Python练习实例017
问题:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : Ma Yi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2020-06-19 # Name : demo017 # Software : PyCharm # Note : 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 # 入口函数 if __name__ == '__main__': s = '''Lei Feng network learned that novel coronavirus pneumonia cases were confirmed in Beijing on the 17 th, and the epidemic prevention work is still grim in the past 21 cases.''' letter = space = digit = other = 0 for i in s: if i.isdigit(): # 数字 digit += 1 elif i.isalpha(): # 英文字母 letter += 1 elif i.isspace(): # 空格 space += 1 else: # 其它 other += 1 print("Letter:%d\tSpace:%d\tDigit:%d\tother:%d" % (letter, space, digit, other))
运行结果:
Letter:137 Space:30 Digit:4 other:2