Search code examples
pythoncountdigits

In Python how can I check how many times a digit appears in an input?


For example let say

numbers=input("Enter numbers: ")

If someone inputs 11234458881

How can I make the output

1 occurs 3 times

2 occurs 1 time

3 occurs 1 time

4 occurs 2 times

And so on


Solution

  • Why not use Counter:

    from collections import Counter
    Counter("11234458881")
    

    returns:

    Counter({'1': 3, '8': 3, '4': 2, '3': 1, '2': 1, '5': 1})