Count in Python: How to Use len(), count(), and Counter
If you search for count in Python, you usually want one of five things: count total characters, count words, count how many times a substring appears, count how often a value appears in a list, or count every repeated value at once. The confusing part is that Python uses different tools for each job. Once you know which one matches your goal, the code is short and readable.
Disclosure: This page contains affiliate links. If you buy through them, we may earn a commission at no extra cost to you.
Quick answer
Use len() when you want the total size of something, such as the number of characters in a string or the number of items in a list. Use str.count() when you want the number of non-overlapping matches for a substring inside text. Use list.count() or tuple.count() when you want the number of exact matches for one value in a sequence. Use collections.Counter when you want a full frequency table for everything at once.
For related basics, see character count basics and our writing tools hub.
Which counting method should you use?
| Goal | Best tool | Example | What it returns |
|---|---|---|---|
| Total characters in text | len(text) | len('banana') | 6 |
| Characters without spaces | len(text.replace(' ', '')) | len('a b'.replace(' ', '')) | 2 |
| Count a substring in text | text.count('an') | 'banana'.count('an') | 2 non-overlapping matches |
| Count one value in a list | items.count(x) | [1, 2, 2, 3].count(2) | 2 exact matches |
| Count one value in a tuple | values.count(x) | (1, 1, 2).count(1) | 2 exact matches |
| Count every value at once | Counter(items) | Counter(['a', 'b', 'a']) | {'a': 2, 'b': 1} |
| Count unique values | len(set(items)) | len(set([1, 2, 2, 3])) | 3 unique values |
The main idea is simple: len() measures size, count() measures occurrences of one target, and Counter measures frequencies for many targets.

Tighten copy after you count it
Use QuillBot to shorten or expand drafts once Python shows you the target length.
Try QuillBotHow to count in Python step by step
1. Count characters in a string
If you want the total number of characters, use len(text). For example, len('hello') returns 5. This is the right choice when you care about total length, not repeated matches.
That is why len() is the method most similar to an online character counter. It tells you how many characters are in the full string, including spaces and punctuation. If you want a no-spaces count, remove spaces first and then measure the result.
- Count all characters: len(text)
- Count characters without spaces: len(text.replace(' ', ''))
- Count words with a simple split: len(text.split())
2. Count a letter or substring in text
Use text.count(sub) when you want to know how many times one piece of text appears inside another. In the official Python docs, str.count() returns the number of non-overlapping occurrences. That detail matters. For example, 'aaaa'.count('aa') returns 2, not 3.
You can also limit the search range with start and end positions. For example, 'spam, spam, spam'.count('spam', 5) returns 2. This is useful when you only want to check one slice of a larger string.
Use count() for exact matches, not fuzzy matches. It does not ignore case for you. If you want case-insensitive counting, normalize the text first with lower() or casefold(), then count on the normalized version.
3. Count one item in a list or tuple
Lists and tuples also have a count() method. If you run [1, 2, 2, 3].count(2), Python returns 2 because the value 2 appears twice. This is great for quick checks, validation, and small scripts.
Use it when you only care about one target value. If you need counts for many values, repeatedly calling list.count() becomes clumsy and less efficient than building one frequency table.
4. Count every repeated value with Counter
When you need a full summary, use from collections import Counter. A Counter stores values as keys and their counts as values, which makes it the standard Python tool for frequency analysis. For example, Counter('banana') gives you a count for each character, and Counter(words).most_common(3) gives you the top three results.
This is usually the best choice for duplicate detection, quick reporting, and ranking the most common items in a list of words, tags, or categories.
5. Count words in a sentence or paragraph
The fastest simple approach is len(text.split()). It works well for clean text separated by standard spaces. It is fine for rough blog drafting, captions, and quick checks. Just remember that punctuation, multiple spaces, line breaks, and special token rules can change what should count as a word in a stricter workflow.
If you need a more precise definition of a word, you usually move beyond plain split() and define the rule first. That is the real lesson here: a counting method is only as good as the thing you decided to count.
6. Count unique values
Sometimes you do not want total occurrences. You want to know how many distinct values exist. In that case, len(set(items)) is the quick answer because a set removes duplicates before you measure the result. For example, len(set([1, 2, 2, 3])) returns 3.
This is useful when you want to know how many different tags, words, IDs, or categories appear in data, even if some of them repeat many times.
Mini recipes you can reuse
- Find how many times a character appears: text.count('a')
- Find how many items are in a list: len(items)
- Find how many times one list value appears: items.count(target)
- Find the most common item: Counter(items).most_common(1)
- Find the number of different values: len(set(items))
If your real goal is content measurement, not just code practice, this sequence works well: count characters with len(), count repeated phrases with str.count(), and use Counter when you want a frequency breakdown.
Mistakes to avoid
- Using count() when you really need len(). len('banana') returns 6 because it measures the whole string. 'banana'.count('a') returns 3 because it measures one target.
- Forgetting that str.count() is non-overlapping. This is why 'aaaa'.count('aa') returns 2.
- Assuming count() is case-insensitive. It matches exactly, so 'Python'.count('p') returns 0 unless you normalize case first.
- Using list.count() over and over for a full frequency report. If you need counts for many values, Counter is usually the cleaner approach.
- Treating split() as a perfect linguistic word counter. It is a good quick estimate, not a full NLP solution.
When each option is best
Choose len() when the question starts with 'how long' or 'how many total'. Choose str.count() when the question starts with 'how many times does this substring appear'. Choose list.count() when you care about one exact value in one sequence. Choose Counter when you want the whole picture instead of one isolated answer.
That decision tree is what most articles on this topic miss. They explain one method in isolation, but real users are usually comparing several ways to count and trying to avoid picking the wrong one.
A practical next step for writers and marketers
If you use Python to measure titles, descriptions, captions, or article drafts, the next problem is often editing the text after you count it. That is where QuillBot can fit naturally: it can help shorten or expand copy, improve grammar, and adjust tone once your script shows that a draft is too long or too short. A simple follow-up is to rewrite text to fit character targets faster after you have measured it in Python. It is most useful for students, marketers, and non-native writers who already know the target they need to hit.
FAQ
What is the difference between len() and count() in Python?
len() returns total size. count() returns how many times one value or substring appears.
How do you count characters in Python?
Use len(text) for all characters, or len(text.replace(' ', '')) if you want to ignore spaces.
How do you count words in Python?
For a quick estimate, use len(text.split()). It works well for simple text separated by spaces.
How do you count duplicates in a list?
Use Counter(list_name) when you want counts for every repeated value, or list_name.count(x) if you only care about one value.
Does count() work on tuples?
Yes. Python sequence types support count(value), so tuples can count exact matches the same way lists do.
Does str.count() count overlapping matches?
No. In Python's official docs, str.count() counts non-overlapping occurrences only.
How do you count unique items in Python?
Use len(set(items)) when you want the number of distinct values rather than the number of total values.
Conclusion
If you remember one rule, make it this: use len() for total length, count() for one target, and Counter for a full frequency view. That covers almost every common counting task in Python. Start with the smallest tool that matches your goal, then move up to Counter when the task gets broader.
A good practice exercise is to test the same sample text or list with all four approaches. Once you see the different outputs side by side, the distinction becomes obvious and you will pick the right method much faster in real work.