Interview question: Determine the number of numerical digits in a string

Crystal X
4 min readMay 18, 2023

If you are intending to become a professional Python programmer then it is highly likely that you will need to go on a coding interview at some point in your quest. String operations are quite common interview questions, so it is a good idea to know the algorithms that solve the problems you will be given during an interview.

One such question is how to determine the number of numerical digits in a string. In this blog post I have presented two methods to solve the problem:-

  1. Use a list of digits to accompany the string.
  2. Use Python’s inbuilt function to determine if the character is a digit.

The psuedocode for using the first method of determining if a character in a string is a digit is:-

  1. Define the list, digit_lst, which is each digit from 0 to 9 in string format.
  2. Define the string that will need to be checked.
  3. Define the function, count_num, which accepts the variables, string and digit_lst, as input.
  4. In the function, define the variable, count, and initialise it to 0.
  5. In the function, create a for loop that will iterate through the length of the string. If the iterated element of the string is in digit_list then…

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

No responses yet