Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a long positive integer

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading