Python Forum
Counting the number of letters in a string alphabetically
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting the number of letters in a string alphabetically
#2
from collections import Counter
import string

test_string = 'Sentence Statistics'

counter = Counter(test_string)

for item, count in counter.most_common():
    if item in string.ascii_letters:
        print(f'{item} appeared {count} times')
Output:
t appeared 4 times e appeared 3 times S appeared 2 times n appeared 2 times c appeared 2 times i appeared 2 times s appeared 2 times a appeared 1 times
Reply


Messages In This Thread
RE: Counting the number of letters in a string alphabetically - by Yoriz - Mar-06-2019, 10:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Extracting Version Number from a String britesc 2 1,087 May-31-2023, 10:20 AM
Last Post: britesc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,852 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,195 May-02-2021, 03:45 PM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,498 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Counting number of words and organize for the bigger frequencies to the small ones. valeriorsneto 1 1,666 Feb-05-2021, 03:49 PM
Last Post: perfringo
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,413 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  counting vowels in a string project_science 3 2,561 Dec-30-2020, 06:44 PM
Last Post: buran
  Replacing a words' letters in a string cananb 2 3,457 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Counting Number of Element in a smart way quest 2 2,005 Nov-09-2020, 10:24 PM
Last Post: quest
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,179 Aug-17-2020, 09:59 AM
Last Post: Tecuma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020