def count_letter_occurrences(input_string):
"""
Counts the occurrences of each letter in a string and stores them in a dictionary.
Args:
input_string: The string to analyze.
Returns:
A dictionary where keys are letters and values are their counts.
"""
letter_counts = {}
for char in input_string:
# Consider only alphabetic characters and convert to lowercase
if char.isalpha():
char_lower = char.lower()
if char_lower in letter_counts:
letter_counts[char_lower] += 1
else:
letter_counts[char_lower] = 1
return letter_counts
# Example usage:
my_string = "Hello World! This is a Test String."
occurrences = count_letter_occurrences(my_string)
print(f"The occurrences of each letter in '{my_string}' are:")
for letter, count in occurrences.items():
print(f"'{letter}': {count}")
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి