关于python辨别数据类型可以用python type()方法,那么想要查看一串字符中每项类型,并逐一输出要怎么处理?看下我是怎么处理的
习题要求:输入一行字符,分别统计其中英文字母、数字、空格、和其他字符的格式
1 string = input("输入要统计的内容:") 2 letter,digit,space,other = 0,0,0,0 3 for i in string: 4 if i.isalpha(): #str.isalpaha()判断是不是字母,返回True/False 5 letter += 1 6 elif i.isdigit(): #str.isdigit()判断是不是字母,返回True/False 7 digit += 1 8 elif i.isspace(): #str.isspace()判断是不是字母,返回True/False 9 space += 1 10 else: 11 other += 1 12 print(letter,digit,space,other)
输入要统计的内容:abdcdeg12345 sge2 ys1 12 7 2 0