67 232
Assignments Done
99,2%
Successfully Done
In November 2018

Answer to Question #4487 in Python for Eric

Question #4487
Write a function count_digits (s) that counts the number of digits in the string s and returns
answer (an integer).
This function is internally using a for loop(in Python language, and keep it as simple as possible) please
Expert's answer
def count_digits (s):
cnt=0
for i in range(len(s)):
if
(s[i]>='0') and (s[i]<='9'):
cnt=cnt+1
return cnt

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Cramaster
27.10.11, 06:46

def count_digits(s):
swithoutdigits = s.translate(None, '1234567890')
digitcount = len(s) - len(swithoutdigits)
return digitcount

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions