每日一程-17.统计字符串中的字母个数和数字个数

Author: Notus([email protected])
Create: 2019-02-24
Update: 2019-02-24

统计字符串中的字母个数和数字个数

环境

Python version: 3.7.1

代码如下(a.py)

'''
    利用函数对输入的字母和数字计数
    @Author: Notus([email protected])
    @Create: 2019-02-24
    @Update: 2019-02-24
    @Version: 0.1
'''

import string

def letterCount(S):
    '''对字母计数'''
    count = 0
    for s in S:
        if s.lower() in string.ascii_lowercase:
            count += 1
    return count

def digitCount(S):
    '''对数字计数'''
    count = 0
    for s in S:
        if s in string.digits:
            count += 1
    return count

s = input("请输入任意内容:")
print("输入的数总长 {}, 其中含字母 {} 个, 数字 {} 个".format(len(s), letterCount(s), digitCount(s)) )

运行

C:\Users\Notus\Desktop>python a.py
请输入任意内容: perw 5;6okofcva 2309z9k 6
输入的数总长 26, 其中含字母 13 个, 数字 8 个

猜你喜欢

转载自www.cnblogs.com/leo1875/p/10428255.html