Python Forum
Counting the number of occurrences of characters in a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Counting the number of occurrences of characters in a string (/thread-27678.html)



Counting the number of occurrences of characters in a string - nsadams87xx - Jun-16-2020

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?


RE: Counting the number of occurrences of characters in a string - bowlofred - Jun-16-2020

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.