Python Program: Counting letters in a string
Python Counter Data Type: Exercise-1 with Solution
Write a Python program to create a 'Counter' of the letters in the string "Python Exercise!".
Sample Solution:
Code:
from collections import Counter
text = "Python Exercise!"
letter_counter = Counter(text)
print("Letter Counter:")
for letter, count in letter_counter.items():
if letter.isalpha():
print(f"{letter}: {count}")
Output:
Letter Counter: P: 1 y: 1 t: 1 h: 1 o: 1 n: 1 E: 1 x: 1 e: 2 r: 1 c: 1 i: 1 s: 1
In the exercise above "Counter" class from the "collections" module to count the occurrences of each letter in the given string. It then iterates through the items in the "Counter" and prints the counts of letters that are alphabetic characters.
Flowchart:

Previous: Python Extended Data Type Counter Exercises Home.
Next: Python Program: Counting common elements in a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Summing a sequence of numbers (calculating the sum of zero to ten with skips):
>>> l = range(0,10,2) >>> sum(l) 20
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook