How can I get the total number of characters in the given string that are digits?

python, string

Solution

Try this:

len("ABC123")

Simple as pie. It might behoof you to read the documentation regarding `len`.

Edit Your original post was ambiguous about whether you wanted the total length or the number of digits. Seeing as you want the latter, I should tell you that there are a million ways of doing it, here are three:

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

Problem

How do I count the number of digits in a string? For example: ``` >>> count_digits("ABC123") ``` should return 3.

Original source