如何计算字符串中的位数?
例如:
>>> count_digits("ABC123")
应该返回3。
尝试这个:
len("ABC123")
简单如馅饼。您可能应该阅读文档 http://docs.python.org/library/functions.html#len关于len.
len
Edit您原来的帖子对于您想要总长度还是位数不明确。鉴于您想要后者,我应该告诉您,有一百万种方法可以做到这一点,以下是三种:
s = "abc123" print len([c for c in s if c.isdigit()]) print [c.isdigit() for c in s].count(True) print sum(c.isdigit() for c in s) # I'd say this would be the best approach