CharacterCounts["string"]
gives an association whose keys are the distinct characters in string, and whose values give the number of times those characters appear in string.
CharacterCounts["string",n]
gives counts of the distinct n-grams consisting of runs of n characters in string.
CharacterCounts[{"string1","string2",…},…]
gives the counts for each of the stringi.
CharacterCounts
CharacterCounts["string"]
gives an association whose keys are the distinct characters in string, and whose values give the number of times those characters appear in string.
CharacterCounts["string",n]
gives counts of the distinct n-grams consisting of runs of n characters in string.
CharacterCounts[{"string1","string2",…},…]
gives the counts for each of the stringi.
Details and Options
- CharacterCounts[string,n] counts n-grams that start at every position in string.
- CharacterCounts has the option IgnoreCase. With the setting IgnoreCaseTrue, characters are in effect all converted to lower case before being counted.
- CharacterCounts[BioSequence["type","seq"]] will give the characters of the string "seq".
Examples
open all close allBasic Examples (3)
Count the distinct characters in a string:
CharacterCounts["banana"]Count the distinct 2-gram sequences in a string:
CharacterCounts["ababcab", 2]Count the distinct characters of each string in a list:
CharacterCounts[{"hello", "world", "!"}]Scope (3)
Character counts include all characters, including numbers, punctuation and whitespace:
CharacterCounts["a1b2c1"]CharacterCounts["TK-421;TK-412", 2]CharacterCounts["right said Fred"]Count the distinct 2-gram sequences of each string in a list:
CharacterCounts[{"banana", "tomato"}, 2]Find the characters of a biomolecular sequence:
CharacterCounts[BioSequence["DNA", "ACCCAAGCTTAA"]]Options (2)
IgnoreCase (2)
The default setting IgnoreCase->False counts uppercase and lowercase characters independently:
CharacterCounts["Alabama", IgnoreCase -> False]Use IgnoreCase->True to count all occurrences of a letter, regardless of case:
CharacterCounts["Alabama", IgnoreCase -> True]Count n-grams regardless of case:
CharacterCounts["abABabABabABabABabAB", 2, IgnoreCase -> True ]CharacterCounts["abABabABabABabABabAB", 2, IgnoreCase -> False]Applications (1)
Properties & Relations (1)
CharacterCounts counts the distinct characters in a string, including numbers, punctuation and whitespace:
CharacterCounts["up-to-date documentation"]LetterCounts includes only the letter characters:
LetterCounts["up-to-date documentation"]Neat Examples (2)
Find the 20 most frequently occurring characters in a body of text:
Take[CharacterCounts[ResourceData["Gettysburg Address"]], 20]Do the same for 3-character sequences:
Take[CharacterCounts[ResourceData["Gettysburg Address"], 3], 20]Make whitespace counts easier to see:
KeyMap[Replace[{" " -> "␣", " " -> "\[TabKey]"}]]@CharacterCounts["a b c"]Text
Wolfram Research (2015), CharacterCounts, Wolfram Language function, https://reference.wolfram.com/language/ref/CharacterCounts.html (updated 2024).
CMS
Wolfram Language. 2015. "CharacterCounts." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/CharacterCounts.html.
APA
Wolfram Language. (2015). CharacterCounts. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CharacterCounts.html
BibTeX
@misc{reference.wolfram_2026_charactercounts, author="Wolfram Research", title="{CharacterCounts}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/CharacterCounts.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_charactercounts, organization={Wolfram Research}, title={CharacterCounts}, year={2024}, url={https://reference.wolfram.com/language/ref/CharacterCounts.html}, note=[Accessed: 13-June-2026]}