Python | 统计一串字符中英文字母、空格、数字和其它字符的个数
代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- inputString = raw_input('请输入字符串:') eng = 0 number = 0 space = 0 other = 0 for i in inputString: if i.isalpha(): eng += 1 elif i.isspace(): space += 1 elif i.isdigit(): number += 1 else: other += 1 print 'eng=%d,space=%d,number=%d,other=%d' % (eng, space, number, other)
输出:
请输入字符串:www. 1 6 3 .com
eng=6,space=4,number=3,other=2