题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
num=input() # 1.初始化统计值均为0 sum1,sum2,sum3,sum4=0,0,0,0 # 2.遍历字符串 for i in num: # (1)如果字符是英文字母,sum1加1 if( i >='a' and i<='z') or (i>='A' and i<='Z'): sum1=sum1+1 # (2)如果字符是空格,sum2加1 elif i==' ': sum2=sum2+1 # (3)如果字符是数字,sum3加1 elif i>='0' and i<='9': sum3=sum3+1 # (4)如果字符是其他字符,sum4就加1 else: sum4=sum4+1 # 3.依次打印sum1,sum2,sum3,sum4 print(sum1) print(sum2) print(sum3) print(sum4)