letter,space,digit,other=0,0,0,0
s = input('input a string:\n')
for c in s:
if c.islower() or c.isupper():
letter +=1
elif c.isspace():
space +=1
elif c.isdigit():
digit +=1
else:
other +=1
print("字母数:%d\n空格数:%d\n数字数:%d\n其他字符数:%d\n"%(letter,space,digit,other))
在py里面有
-
curses.ascii.
isalpha
(
c
)
-
Checks for an ASCII alphabetic character; it is equivalent to isupper(c) or islower(c).
-
用 .isalpha()算出来的会把汉字也加里面,所以other = 0,letter 多4
-
但是标准答案里面给的。。挺质朴的。
-
if (c<='Z' and c>='A') or (c<='z' and c>= 'a'):
-
嗯。。。好吧。