如何用Python统计字符串(引用ASCII码)【两种方法】

要求实现根据输入的字符串,统计其中大写字母、小写字母、数字、字符各有多少个

重要步骤提示

0-9的ASCII数字的ASCII码值取值范围为48-57;

a-z小写英文字母的取值范围为97-122;

A-Z大写英文字母的取值范围为65-90;

Len()、append()方法的使用

ord()函数获取字符对应的ASCII码值

方法一

#引到用户输入字符
list1 = list(input('请输入一行字符:'))
#定义统计量初始个数
big_white = 0
small_white = 0
number = 0
others = 0
#循环查找变量,进行统计
for i in list1:
    #查找大写字母
    if (i >= 'A') and (i <= 'Z'):
        #找到个数加一
        big_white  += 1
    #查找小写字母
    elif (i >= 'a') and (i <= 'z'):
        #找到个数加一
        small_white  += 1
    #查找数字
    elif (i >= '0') and (i <= '9'):
        #找到个数加一
        number += 1
    #查找字符
    else:
        #找到个数加一
        others += 1
#统计输出
print('大写字母个数:%s' %big_white )
print('小写字母个数:%s' %small_white)
print('数字个数:%s' %number)
print('其它字符个数:%s' %others)

循环遍历列表,if条件查找,最后格式化输出-----使用ASCII码‘  ’引出

#引到用户输入字符
list1 = list(input('请输入一行字符:'))
#定义统计量初始个数
big_white = 0
small_white = 0
number = 0
others = 0
#循环查找变量,进行统计
for i in list1:
    #查找大写字母
    if (i >= 'A') and (i <= 'Z'):
        #找到个数加一
        big_white  += 1
    #查找小写字母
    elif (i >= 'a') and (i <= 'z'):
        #找到个数加一
        small_white  += 1
    #查找数字
    elif (i >= '0') and (i <= '9'):
        #找到个数加一
        number += 1
    #查找字符
    else:
        #找到个数加一
        others += 1
#统计输出
print('大写字母个数:%s' %big_white )
print('小写字母个数:%s' %small_white)
print('数字个数:%s' %number)
print('其它字符个数:%s' %others)

方法二

#引到用户输入字符
list1 = list(input('请输入一行字符:'))
#定义统计结果
big_white = []
small_white = []
number = []
others = []
#循环查找数据,list1[i]中i为脚标
for i in range(len(list1)):
    # 查找大写字母,用ord转化查找
    if ord(list1[i]) in range(65, 91):
        #找到存进相应列表
        big_white.append(list1[i])
    # 查找小写字母,用ord转化查找
    elif ord(list1[i]) in range(97, 123):
        # 找到存进相应列表
        small_white.append(list1[i])
    # 查找数字,用ord转化查找
    elif ord(list1[i]) in range(48, 58):
        # 找到存进相应列表
        number.append(list1[i])
    # 查找其余字符,用ord转化查找
    else:
        # 找到存进相应列表
        others.append(list1[i])
print('大写字母个数:%s' % len(big_white))
print('小写字母个数:%s' % len(small_white))
print('数字个数:%s' % len(number))
print('其它字符个数:%s' % len(others))
循环遍历列表中的i表示脚标)




#引到用户输入字符
list1 = list(input('请输入一行字符:'))
#定义统计结果
big_white = []
small_white = []
number = []
others = []
#循环查找数据,list1[i]中i为脚标
for i in range(len(list1)):
    # 查找大写字母,用ord转化查找
    if ord(list1[i]) in range(65, 91):
        #找到存进相应列表
        big_white.append(list1[i])
    # 查找小写字母,用ord转化查找
    elif ord(list1[i]) in range(97, 123):
        # 找到存进相应列表
        small_white.append(list1[i])
    # 查找数字,用ord转化查找
    elif ord(list1[i]) in range(48, 58):
        # 找到存进相应列表
        number.append(list1[i])
    # 查找其余字符,用ord转化查找
    else:
        # 找到存进相应列表
        others.append(list1[i])
print('大写字母个数:%s' % len(big_white))
print('小写字母个数:%s' % len(small_white))
print('数字个数:%s' % len(number))
print('其它字符个数:%s' % len(others))

【代码思路】

通过for循环遍历输入的字符串,通过if嵌套判断遍历的字符是否在大写字母、小写字母、空格、数字、其他字符所对应的ASCII码值范围,满足条件则将其将入相应的列表中,最后通过输出列表的长度从而得到相应字符的个数

有问题欢迎留言提问^V^一起交流进步

关注我更新更多初学实例

你可能感兴趣的:(python,前端,linux)