输入一段文字,统计数字,字符,空格数量

while True:
    msg = input("输入一段文字")
    if not msg: continue
    # str_count, int_count, space_count, special_count = 0, 0, 0, 0
    str_count = int_count = space_count = special_count = 0
    for i in msg:
        if i.isdigit():
            int_count += 1
        elif i.isalpha():
            str_count += 1
        elif i.isspace():
            space_count += 1
        else:
            special_count += 1
    print(f"数字:{int_count}个,字母:{str_count}个,空格:{space_count}个,其他:{special_count}个,")

  

posted @ 2023-05-16 15:46  sangern  阅读(84)  评论(0)    收藏  举报