-1

how could i count the 4 in an integer with python.

The integer is 3 to the 10000. And I would like to know how often is the 4 in this integer.

maybe something like: count 4 in 3**10000

Manu
  • 31
  • 4
  • 1
    You should show the value and type you have and what result you want, this is very unclear as it stands. – Alex K. Jan 02 '18 at 14:53
  • 1
    1) calculate the value and save as a string. 2) get count of "4" in string. – OldProgrammer Jan 02 '18 at 14:53
  • yes but can I do that in Python ? – Manu Jan 02 '18 at 14:54
  • Convert each integer to a string using `str(...)`, then use the suggestions of [this thread](https://stackoverflow.com/q/4664850/3082472) to find all occurrences of the string `'4'`. Also, as you're new here, I'd like to recommend reading https://stackoverflow.com/help/how-to-ask. Cheers! – akraf Jan 02 '18 at 14:54
  • Are you asking us to do your homework? – Dominique Jan 02 '18 at 15:20

1 Answers1

1

The following works:

len(str(3**10000).split('4'))-1

or as suggested below:

str(3**10000).count('4')

both give 495.

DrTRD
  • 1,641
  • 1
  • 13
  • 18
  • this is like counting male population in a city by counting how many houses have a family with a husband/dad, its correct but kinda inefficient – Mixone Jan 02 '18 at 15:01
  • 2
    You know `str.count` also exist – Taku Jan 02 '18 at 15:01