输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import string
s = raw_input('input a string:\n')
letters = 0
space = 0
digit = 0
others = 0
for c in s:
    if c.isalpha():
        letters += 1
    elif c.isspace():
        space += 1
    elif c.isdigit():
        digit += 1
    else:
        others += 1
print 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,othe

 

Python 判断字符属于数字、字母、空格的方法:

字符 c.isalpha()

空格 c.isspace()

数字 c.isdigit()

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-12
  • 2021-11-18
  • 2021-06-05
猜你喜欢
  • 2021-06-15
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
  • 2021-12-26
  • 2022-12-23
  • 2021-10-11
相关资源
相似解决方案