Python统计字符

2021-12-09  本文已影响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))
上一篇 下一篇

猜你喜欢

热点阅读