Python Forum

Full Version: Counting the number of occurrences of characters in a string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone,

I was presented with a challenge where I need to code a function that takes a string and then counts the number of times each character occurs in the string. Here's an example:

Input: "occurrences"

output: o1c3u1r2e2n1s1

I know the rules about no effort posts but I don't really know where to start other than taking the string, passing it into the function, and then putting the string into a list. After that do I create another list of unique characters to count them? Use a dictionary to store the results? How would I go about printing out the result like such?
Strings are already iterators in python. Take a look at the standard library and the Counter type. It lets you easily count from an iterator.

I think if you play with the examples, you'll have some ideas.