I am sorry if this question may sound a bit trivial for those who are more expert than me. If I have to create a long positive integer as well as find the number of zeros (with the folowing instruction as an hint: change the number to a string), do you know what I am supposed to do?
I am very sorry for this kind of question but I have just started moving my first step towards Python.
Thanks
CodePudding user response:
you can convert an int
to a str
using str()
and then count the number of zeros,
this can be done using the findall()
function from re
import re
len(re.findall('0', str(number)))
findall()
returns a list of all the occurrences that match the regex in the first argument, here '0' will only match a zero, the second argument is the string to check, here we pass the number cast as a string using str()
. as findall()
returns a list of occurrences we can pass the function as an argument to len()
to get the number of occurrences
you can read more here