0

My program that takes a string as an input from the user and counts the frequency of each character using the dictionary. Input:

Python programming is fun

Expected output:

{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}

My code:

string = input().lower()
dicx = {}
count = 0
for i in string:
    dicx['i'] = ''

print(dicx)
Shakiib
  • 57
  • 1
  • 6
  • Does this answer your question? [Find count of characters within the string in Python](https://stackoverflow.com/questions/40950905/find-count-of-characters-within-the-string-in-python) – Hoai Nam Sep 03 '22 at 15:19

6 Answers6

2

Use collections.Counter

dicx = collections.Counter(string.lower())
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50
0

You can iterate over string and update the dictionary accordingly and also there's no need of any count variable.

test_str = input().lower()
dicx = {} 
  
for i in test_str: 
    if i in dicx: 
        dicx[i] += 1
    else: 
        dicx[i] = 1

print(dicx)
Anurag Singh
  • 126
  • 5
0

Function takes input as string and counts the character and stores them in a dictionary

from typing import Dict


char_dict = {} #type: Dict[str, int]


def char_count(string: str) -> dict:
    new_string = string.lower()
    for c in new_string:
        if c in char_dict:
            char_dict[c] += 1
        else:
            char_dict[c] = 1
    return char_dict


if __name__ == "__main__":
    UserString = input("Enter Input String: ")
    CharCount = char_count(UserString)
    print("Characters Count: ", CharCount)

Example:

Enter Input String: Python programming is fun

Characters Count:  {'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, ' ': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}
karthik
  • 34
  • 5
0

Way 1: For

    symbols = {}
    for s in inp_str.lower():
        if s in symbols:
            symbols[s] += 1
        else:
            symbols.update({s: 1})
    print(symbols)

Way 2: defaultdict

     symbols = defaultdict(int)
     for s in inp_str.lower():
         symbols[s] += 1
     print(symbols)

Way 3: Counter

    symbols = Counter(inp_str.lower())
    print(symbols)
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
0
def charCounter(string):
    empty = {}
    for i in string.lower():
        if i in empty.keys():
            empty[i] += 1
        else:
            empty[i] = 1
    return empty

print(charCounter("Oh, it is python"))
apayziev
  • 3
  • 5
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 04 '22 at 00:57
0
d = {}
test_str = input().lower()

for x in test_str:
    d[x] = d.get(x,0) + 1
print(d)

much more elegant like this

Wayne Gan
  • 39
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '23 at 14:20