首页 > 编程语言 > 详细

Python练习实例017

时间:2020-06-19 09:35:32      阅读:70      评论:0      收藏:0      [点我收藏+]

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

#! /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

 

Python练习实例017

原文:https://www.cnblogs.com/mayi0312/p/13161203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!