python 统计一个字符串中字母、数字及其他字符的个数,返回一个元组

# 统计一个字符串中字母、数字及其他字符的个数,返回一个元组

def tongji(s):

    count1 = 0

    count2 = 0

    count3 = 0

    for i in range(len(s)):

        if (s[i] >= 'a' and s[i] <= 'z') or (s[i] >= 'A' and s[i] <= 'Z'):

            count1 = count1 + 1

        elif (s[i] >= '0' and s[i] <= '9'):

            count2 = count2 + 1

        else:

            count3 = count3 + 1

    return (count1,count2,count3)


s = input("请输入一个字符串:")

print(tongji(s))

猜你喜欢

转载自blog.csdn.net/chen1042246612/article/details/81174490