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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM