Character frequency in a string (Python 3.9)

408 Views Asked by At

How can you count the characters (characters frequency) in a string without using if, while or for?

1

There are 1 best solutions below

0
Cute Panda On

You can use Counter, it returns a dictionary with character as a key and its frequency as its value.

from collections import Counter
x = "abcdasbdd"
print(Counter(x))

Output

Counter({'d': 3, 'a': 2, 'b': 2, 'c': 1, 's': 1})