Created
September 27, 2022 15:36
-
-
Save iKunalChhabra/38fc1054097101f8baf4cd44d7e60d77 to your computer and use it in GitHub Desktop.
Count the number of occurrences of characters in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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