-1

I have this code:


def main():
    test_str = input('Enter word')

    def count_dict(mystring):
        d = {}
        for w in mystring:
            d[w] = mystring.count(w)

        for k in sorted(d):
            print(k + ' : ' + str(d[k]))

    mystring = test_str
    count_dict(mystring)

if __name__ == '__main__':
    main()

and my output is like this:

Enter word: test
Output:
e : 1
s : 1
t : 2

Now the question is how can I add a ' to make it look like this:

'e' : 1
's' : 1
't' : 2
martineau
  • 119,623
  • 25
  • 170
  • 301
anonymous
  • 25
  • 2

1 Answers1

0

Here's my approach: (Diffs. are commented) more about f-strings vs string-format

def main():
    test_str = input('Enter word: ')  # slightly changed representation

    def count_dict(mystring):
        
        d = {}
        for w in mystring:
            d[w] = mystring.count(w)

        for k in sorted(d):
            print(f'("{k}" : {str(d[k])})')  # you may check out python f-strings

    mystring = test_str
    count_dict(mystring)

if __name__ == '__main__':
    main()