计算字符个数并求占比

img

按照图片上的文字如何打出和示例一样的效果,我使用了if-elif-else这个循环语句,在print那里怎么输入。,。,。


str1 = input("【请输入一行字符: 】")
dict1 = {}
sum_number = 0
sum_letter = 0
sum__ = 0
sum_other = 0
sum1 = len(str1)
for i in str1:
    if "a"<= i <= "z" or "A" <= i <= "Z":
        sum_letter += 1
    elif "0" <= i <= "9":
        sum_number += 1
    elif i == ' ':
        sum__ += 1
    else :
        sum_other += 1
print(f"统计如下: \n数字有{sum_number}个, 占{sum_number/sum1*100:.1f}%")
print(f"字母有{sum_letter}个, 占{sum_letter/sum1*100:.1f}%")
print(f"空格有{sum__}个, 占{sum__/sum1*100:.1f}%")
print(f"其他有{sum_other}个, 占{sum_other/sum1*100:.1f}%")

https://blog.csdn.net/weixin_39635459/article/details/110013199?ops_request_misc=&request_id=&biz_id=102&utm_term=Python%E8%AE%A1%E7%AE%97%E5%AD%97%E7%AC%A6%E4%B8%AA%E6%95%B0%E5%B9%B6%E6%B1%82%E5%8D%A0%E6%AF%94&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-110013199.142^v62^js_top,201^v3^control_1,213^v1^control&spm=1018.2226.3001.4187


s = input()
t_num = 0
w_num = 0
s_num = 0
o_num = 0
for t in s:
    if t.isdigit():
        t_num += 1
    elif t.isalpha():
        w_num += 1
    elif t == ' ':
        s_num += 1
    else:
        o_num += 1

print('统计如下:')
print('数字有{}个,占{}%'.format(t_num, round(t_num/len(s), 2) * 100))
print('字母有{}个,占{}%'.format(w_num, round(w_num/len(s), 2) * 100))
print('空格有{}个,占{}%'.format(s_num, round(s_num/len(s), 2) * 100))
print('其他有{}个,占{}%'.format(o_num, round(o_num/len(s), 2) * 100))