In Python: Create a function called countDigits that will count all of the digit variables in a string. Your function should take as an argument the string to search through. It should return the number of digits that are within the string. For instance, given the following string; string str = "1xyz34abc5"; Your function would return 4 because there are 4 digits in the string. HINT: You should investigate the String.isdigit function. It will simplify your code a great deal.

Respuesta :

Answer:

Hi Kyelawright! Please find the answer below.

Explanation:

As the question suggested, we can simply use the isdigit() function in Python to check if the current string is a digit and update the digits counter if it returns true.

import sys;

def countDigits(s):

   digits_count = 0;

   for x in range(0, len(s)):

       if s[x].isdigit():

           digits_count += 1;

   return digits_count;

s = input("Enter a sentence: ");

print(countDigits(s));