Counting Words and Characters
Basic length and frequency measurements.
Counting Words and Characters is a free NLP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Measuring Text First
Before any fancy analysis, you measure text. Simple counts of characters and words already tell you a lot about a document. 📏
Count Characters With len
The len function returns how many characters a string holds, including spaces and punctuation.
text = "NLP is fun"
print(len(text)) # 10Spaces Count Too
Remember that whitespace like spaces and newlines are real characters, so they are included in any character count.
Split Into Words
The split method breaks a string on whitespace and returns a list of words, your simplest path to word counting.
words = "one two three".split()
print(words) # ['one','two','three']Count the Words
Apply len to the list from split and you get the word count of the whole document.
words = text.split()
print(len(words)) # number of wordsSplit Is Naive
Plain split treats hello! as one word because punctuation sticks to letters. Good enough to start, but not perfect.
Count Sentences Roughly
A quick sentence estimate is to count periods, question marks, and exclamation points. It is rough but instantly useful.
n = text.count(".") + text.count("?") + text.count("!")Average Word Length
Divide total letters by word count to get average word length, a small signal about how dense the writing is.
avg = len(text.replace(" ", "")) / len(words)Counting Specific Substrings
The count method tells you how many times a substring appears, handy for tallying a single target word.
print(text.lower().count("the"))Unique Words With a Set
Put the word list into a set and its length is the number of unique words, a measure of vocabulary richness.
unique = set(words)
print(len(unique))Why Counts Matter
These statistics drive real decisions: spam filters flag odd lengths, and readability tools lean on word counts.
Quick Check
Test your grasp of basic Python text counting.
Recap: Counting Text
You can now count characters with len, words with split, unique words with a set, and rough sentences by punctuation. 🔢
Frequently asked questions
Is the “Counting Words and Characters” lesson free?
Yes — the full text of “Counting Words and Characters” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Counting Words and Characters”?
Basic length and frequency measurements. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Counting Words and Characters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Strings, Characters, and Encodings
- Reading Text Files Into Python
- Counting Words and Characters
- Building Your First Word Frequency Table