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