Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
character_counter.py
#!/usr/bin/env python3
########################################################################################
#
# Version
# -------
# v1.0.0
#
# License
# -------
# MIT License
# Copyright (c) 2020-present Hunter T.
#
########################################################################################
"""
Has you input a string of characters, then reports how many of each character
you entered there are in the string.
"""
####[ Imports ]#########################################################################
from collections import Counter
####[ Variables ]#######################################################################
# Counter creates a dictionary that counts the number of similar characters
sentence = Counter(input("What would you like to say? ").lower())
sentence_length = 0
####[ Main ]############################################################################
# Takes Counter dictionary and changes it into a list, then goes through it...
for k, v in sentence.items():
if v > 1:
# Takes vars in parentheses and place its value in the brackets
print("There are {} {}'s in this sentence.".format(v, k))
else:
print("There is {} {} in this sentence.".format(v, k))
sentence_length += v
print("There are {} characters, including spaces, in total.".format(sentence_length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment