D
D
d1ret2018-10-23 01:02:03
Python
d1ret, 2018-10-23 01:02:03

How to extract a number from a number?

For example, there is a natural number, and I need to find out how many digits "4" are in it, for example.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@bickford, 2018-10-23
@d1ret

num = 45644644
str_num = str(num)
count = str_num.count('4')
print(count)  # 5

0
0xD34F, 2018-10-23
@0xD34F

number = 18374908234
digits = str(number)
count = { i: digits.count(str(i)) for i in range(10) }

print(count)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question