美文网首页
Python统计字符

Python统计字符

作者: 火卫控 | 来源:发表于2021-12-09 15:25 被阅读0次

    Python统计字符
    代码短小精悍

    结果如下:


    结果

    代码如下:

    # -*- coding: utf-8 -*-
    # @Time : 2019/7/30 12:40
    # @Author : Administrator 艾强云
    # @File : tongjizifu.py
    # @Software: PyCharm
    import string
    s = input('input a string:\n')
    letters = 0
    space = 0
    digit = 0
    others = 0
    for c in s:
        if c.isalpha():
            letters += 1
        elif c.isspace():
            space += 1
        elif c.isdigit():
            digit += 1
        else:
            others += 1
    print('char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others))
    

    相关文章

      网友评论

          本文标题:Python统计字符

          本文链接:https://www.haomeiwen.com/subject/tvtyxrtx.html