Counting Occurrences in Python

Regular Expressions Mar 19, 2026

Counting Occurrences in Python

Counting how many times a substring or pattern appears in text is one of the most common and powerful operations in data science. Whether you are analyzing word frequencies, counting error codes in logs, measuring keyword density, or preparing features for machine learning models, efficient counting techniques save time and improve accuracy. In 2026, Python offers several elegant methods — from simple string methods to Counter and regex-based counting — that make this task fast and readable.

TL;DR — Best Counting Methods

  • str.count(sub) → simple substring count
  • collections.Counter → count multiple items efficiently
  • re.findall(pattern) + len() → count regex matches
  • re.finditer(pattern) → count with positions
  • pandas .str.count() → vectorized counting on DataFrames

1. Basic Counting with str.count()

text = "Data science is great for data analysis and data visualization"

print(text.count("data"))           # 2 (case-sensitive)
print(text.lower().count("data"))   # 3 (case-insensitive)

2. Powerful Counting with collections.Counter

from collections import Counter

words = text.lower().split()
word_count = Counter(words)
print(word_count.most_common(5))    # Top 5 most frequent words

3. Regex-Based Counting

import re

# Count specific patterns (\b = word boundary, so "database" would not count)
pattern = r"\bdata\b"
matches = re.findall(pattern, text.lower())
print(len(matches))

# Count with positions using finditer
for match in re.finditer(r"\bdata\b", text.lower()):
    print(f"Found at position {match.start()}")

4. Real-World Data Science Examples with Pandas

import pandas as pd

# Demo file so this example runs as-is (in your project this is your real export)
pd.DataFrame({
    "customer_name": ["  alice smith ", "BOB JONES", " carol diaz Inc."],
    "first_name": ["Alice", "Bob", "Carol"],
    "last_name": ["Smith", "Jones", "Diaz"],
    "email": [" Alice@Example.com", "bob@company.org", "carol@data.net "],
    "region": ["N.Y.", "California", "N.Y."],
    "company": ["Acme Inc.", "Globex Inc.", "Initech"],
    "product_code": ["EL-101 ", "BK-202", "#HM-303"],
    "product_category": ["electronics", "books", "home"],
    "date_str": ["2026-03-19", "2025-11-02", "2026-01-15"],
    "description": ["Premium laptop for machine learning and data science",
                    "Data book: data cleaning with Python",
                    "Contact sales@shop.com for premium support"],
    "log": ["2026-03-19 - INFO - login", "2026-03-19 - ERROR - timeout", "2026-03-20 - INFO - logout"],
}).to_csv("customer_data.csv", index=False)

df = pd.read_csv("customer_data.csv")

# Count keyword occurrences in descriptions
df["data_count"] = df["description"].str.lower().str.count("data")

# Count multiple patterns at once
df["keyword_count"] = df["description"].str.lower().str.count(r"data|science|machine learning")

5. Best Practices in 2026

  • Use str.count() for simple substring counting
  • Use Counter when counting multiple different items
  • Use regex counting for complex patterns
  • Always normalize case before counting unless case-sensitivity is required
  • Use pandas .str.count() for vectorized counting on large datasets

Counting Words Versus Counting Substrings

str.count counts non-overlapping occurrences of a substring, so "data" is also found inside "database". When you want whole words only, use a regex with word boundaries, \bdata\b, or split the text into words first.

Counts are case sensitive. Lowercase the text first or use the re.IGNORECASE flag. In pandas, .str.count accepts a regex pattern and returns a number per row.

For counting how often each distinct word appears, collections.Counter on a list of words is the most direct tool.

Worked Example: Word Frequency

We compare substring counts with whole-word counts.

import re
from collections import Counter

text = "Data is great. Database data and big DATA."

print(text.lower().count("data"))
print(len(re.findall(r"\bdata\b", text, flags=re.IGNORECASE)))

words = re.findall(r"[a-z]+", text.lower())
print(Counter(words).most_common(2))

The substring count gives 4 because it includes "database". The word-boundary regex gives 3, and Counter shows that "data" is the most common word.

Practical Tips

Decide early whether you need substring counts or word counts, and name the variable accordingly, such as substring_hits or word_hits. That small habit prevents reports from mixing the two measures.

For pandas columns, .str.count with a word-boundary pattern gives a per-row word count in one vectorised call.

Normalise punctuation first so words at the end of sentences are counted too.

Common Mistakes

  • Counting substrings when whole words are needed.
  • Forgetting that counts are case sensitive.
  • Expecting count to find overlapping matches.
  • Counting punctuation as part of words.

Quick Summary

  • count finds non-overlapping substrings.
  • \b limits matches to whole words.
  • Normalise case before counting.
  • Counter gives word frequencies.

Conclusion

Counting occurrences is a foundational skill that bridges basic string operations and advanced Regular Expressions. In 2026 data science projects, the combination of str.count(), Counter, regex findall(), and pandas .str.count() gives you fast, flexible, and scalable tools for text analysis. These techniques make your data cleaning, feature engineering, and pattern detection pipelines cleaner and more efficient.

Next steps:

  • Review your current text columns and apply counting techniques to extract frequency features

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices