find_repeated_characters_in_string.py


Sign up Free. Don't forget to check out our challenges, lessons, solve and learn series and more ...


Code Snippet

import collections #this import collections statement is important

def main():
    string="I love python"
    d=collections.defaultdict(int)
    for o in string:
        d[o] +=1

    for o in sorted(d, key=d.get, reverse=True):
        if d[o] >1:
            print("%s %d" % (o,d[o]))

    print()
    print()
    string="o and two more o's, I love python"
    d=collections.defaultdict(int)
    for o in string:
        d[o] +=1

    for o in sorted(d, key=d.get, reverse=True):
        if d[o] >1:
            print("%s %d" % (o,d[o]))
    
main()
                    

Try it yourself