Issue
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 following instruction as a hint: change the number to a string), do you know what I am supposed to do? Actually, these are some of the notions and examples that I'm focusing on to solve this problem.
I do not know whether the blue highlighted lines may be suitable for the assignment. There is no specified other info (if you are wondering, I have downloaded python 3.10, although typing the command python onto the panel of commands prompt, it pops up that the python's version in use is 3.6.6).
I am very sorry for this kind of question but I have just started moving my first step towards Python.
Thanks
Solution
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
Answered By - Patrick Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.