python计算字符串中小写字符数目

用python写一段程序,计算字符串中比如 This is a sentence. 这句话,怎样count 小写字符数目。

第1个回答  2012-02-24
较简单的办法:
total = 0
sentence = "This is a sentence."
for word in sentence:
if word.islower():
total += 1
还可以用ord()判断字符值来实现。
第2个回答  2012-02-24
>>> import re
>>> patt = re.compile('[a-z]', re.X)
>>> s = 'This is a sentence.'
>>> print len(patt.findall(s))
14
>>>

or
>>> import stringg
>>> def islower(c):
... return c in string.lowercase
...
>>> len(filter(islower, a))
第3个回答  2012-02-29
楼上很好很强大,楼楼上更简单一些,都是达人啊。
相似回答