X Xerobit

Letter Counter — Count Letters and Characters in Text

A letter counter tells you how many alphabetic characters are in your text, separate from spaces, digits, and punctuation. Here's when this matters and how to implement it.

Mian Ali Khalid · · 5 min read
Use the tool
Word Counter
Count words, characters, sentences, paragraphs, and lines. Reading time estimate, char-limit indicators for X, LinkedIn, meta titles, and more.
Open Word Counter →

A letter counter counts only alphabetic characters (a–z, A–Z) in a text, ignoring spaces, digits, and punctuation. This is distinct from a character counter, which counts everything.

The Word Counter counts characters with and without spaces. For letter-only counting, run the character count and subtract spaces, digits, and punctuation — or use the frequency view to count just alphabetic characters.

When letter count matters

Cryptography and cipher analysis

When analyzing Caesar cipher, Vigenère cipher, or other substitution ciphers, you need to count only letters — not spaces or punctuation. Spaces and punctuation don’t participate in the substitution, so frequency analysis applies only to the alphabetic character set.

The standard English letter frequency distribution (percentage of letters, not percentage of total characters):

LetterFrequencyLetterFrequency
E12.7%M2.4%
T9.1%W2.4%
A8.2%F2.2%
O7.5%G2.0%
I7.0%Y2.0%
N6.7%P1.9%
S6.3%B1.5%
H6.1%V1.0%
R6.0%K0.8%
D4.3%J0.2%
L4.0%X0.2%
C2.8%Q0.1%
U2.8%Z0.1%

These frequencies apply to the letter portion of text — the raw character count includes spaces (~15–20% of characters in English text) that dilute the frequencies.

License plates and alphanumeric codes

Some data entry systems require exactly N alphabetic characters (separate from digits). A vehicle registration like ABC-123 has 3 letters and 3 digits.

Password strength analysis

Password policies often specify minimum letter counts: “at least 2 uppercase letters and 3 lowercase letters.” Counting letters separately from digits and symbols is part of basic password policy validation.

Language learning

Students counting syllables or analyzing word structure in a foreign language need letter counts to separate the alphabetic content from punctuation.

Counting letters vs characters: the difference

Text: "Hello, World! 123"
Total characters: 18
Characters without spaces: 15
Letters only (a-z, A-Z): 10
Digits only: 3
Punctuation: 2
Spaces: 3

For SEO meta descriptions, you care about total characters (all 18 in this example). For ciphers, you care about letters only (10). For data validation, you might care about each category separately.

Implementing a letter counter

JavaScript

function countLetters(text) {
  const letterRegex = /[a-zA-Z]/g;
  const letters = text.match(letterRegex) || [];
  return letters.length;
}

function countByType(text) {
  return {
    total: text.length,
    letters: (text.match(/[a-zA-Z]/g) || []).length,
    uppercase: (text.match(/[A-Z]/g) || []).length,
    lowercase: (text.match(/[a-z]/g) || []).length,
    digits: (text.match(/\d/g) || []).length,
    spaces: (text.match(/\s/g) || []).length,
    punctuation: (text.match(/[^\w\s]/g) || []).length,
  };
}

const result = countByType("Hello, World! 123");
// { total: 17, letters: 10, uppercase: 2, lowercase: 8, digits: 3, spaces: 2, punctuation: 2 }

Python

def count_by_type(text):
    return {
        'total': len(text),
        'letters': sum(c.isalpha() for c in text),
        'uppercase': sum(c.isupper() for c in text),
        'lowercase': sum(c.islower() for c in text),
        'digits': sum(c.isdigit() for c in text),
        'spaces': sum(c.isspace() for c in text),
        'punctuation': sum(not c.isalnum() and not c.isspace() for c in text),
    }

result = count_by_type("Hello, World! 123")
# {'total': 17, 'letters': 10, 'uppercase': 2, 'lowercase': 8, 'digits': 3, 'spaces': 2, 'punctuation': 2}

Note: isalpha() in Python is Unicode-aware — it returns True for accented characters like é, ñ, ü, and CJK characters. If you only want ASCII letters, use c in string.ascii_letters.

Unicode letter counting

In Python, 'é'.isalpha() returns True. This is generally correct — é is a letter. But for the cipher-analysis use case where you want only ASCII letters that participate in English frequency analysis, filter more strictly:

import string

def count_ascii_letters(text):
    return sum(c in string.ascii_letters for c in text)
    
def count_all_letters(text):
    return sum(c.isalpha() for c in text)

count_ascii_letters("café")   # 3 (c, a, f — not é)
count_all_letters("café")     # 4 (all letters including é)

Letter frequency analysis

For a frequency analysis of which letters appear most often in a text (useful for cipher analysis):

from collections import Counter
import string

def letter_frequency(text, case_sensitive=False):
    if not case_sensitive:
        text = text.lower()
    
    letters = [c for c in text if c in string.ascii_letters.lower()]
    total = len(letters)
    
    if total == 0:
        return {}
    
    freq = Counter(letters)
    return {
        letter: {
            'count': count,
            'percentage': round(count / total * 100, 1)
        }
        for letter, count in freq.most_common()
    }

text = "The quick brown fox jumps over the lazy dog"
freq = letter_frequency(text)
# {'o': {'count': 4, 'percentage': 9.8}, 'e': {'count': 3, 'percentage': 7.3}, ...}

Letter counter for social media copywriting

Some A/B testing frameworks measure ad copy performance by character type distribution. Research from copywriting literature (particularly David Abbott’s agency work) suggests:

  • High letter-to-total ratio: longer words, formal tone
  • Short words + many spaces: punchy, direct copy
  • High punctuation-to-letter ratio: emphasis-heavy, potentially overstated

For display advertising with strict character limits, knowing the letter count separately from punctuation helps when you need to trim copy while preserving meaning — letters are the meaning-carriers, punctuation can often be reduced.

The Word Counter on Xerobit

The Word Counter provides the core metrics needed for most use cases: words, characters with spaces, characters without spaces, sentences, paragraphs, and reading time. For pure letter-only counting, the characters-without-spaces metric is a close proxy (it includes digits and some punctuation, but for typical English text it’s usually ~95% letters).


Related posts

Related tool

Word Counter

Count words, characters, sentences, paragraphs, and lines. Reading time estimate, char-limit indicators for X, LinkedIn, meta titles, and more.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.