Skip to content

Instantly share code, notes, and snippets.

@iKunalChhabra
Created September 27, 2022 15:36
Show Gist options
  • Save iKunalChhabra/38fc1054097101f8baf4cd44d7e60d77 to your computer and use it in GitHub Desktop.
Save iKunalChhabra/38fc1054097101f8baf4cd44d7e60d77 to your computer and use it in GitHub Desktop.
Count the number of occurrences of characters in a string
from collections import Counter
string = "Kunal Chhabra"
search_char = "a"
counter = Counter(string.lower())
print(counter)
# output -> Counter({'a': 3, 'h': 2, 'k': 1, 'u': 1, 'n': 1, 'l': 1, ' ': 1, 'c': 1, 'b': 1, 'r': 1})
print(counter[search_char])
# output -> 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment