count() function - NumPy String Operation Last Updated : 29 Sep, 2025 Comments Improve Suggest changes Like Article Like Report The numpy.char.count() function is used to count the number of non-overlapping occurrences of a substring within each element of a string array. It can also take optional start and end positions to limit the search to a substring range.For Example: This example counts how many times "o" appears in each word. Python import numpy as np arr = np.array(['book', 'moon', 'loop']) res = np.char.count(arr, sub='o') print(res) Output[2 2 2] Explanation: Each word contains two "o" characters, so the result is [2 2 2].Syntaxnumpy.char.count(arr, sub, start=0, end=None)Parameters:arr: array_like -> Input array of strings.sub: str -> Substring to search for.start: int, optional -> Starting position (default 0).end: int, optional -> Ending position (default None, till end of string).Return Value: ndarray -> Array of integers showing the count of substring occurrences.ExamplesExample 1: This example counts the occurrences of "an" in each string. Python import numpy as np arr = np.array(['Sayantan', 'Sayan', 'Sayansubhra']) res = np.char.count(arr, sub='an') print(res) Output[2 1 1] Explanation:"Sayantan" -> contains "an" twice"Sayan" -> contains "an" once"Sayansubhra" -> contains "an" onceExample 2: This example counts how many times "e" appears in city names. Python import numpy as np arr = np.array(['Berlin', 'Venice', 'New Delhi']) res = np.char.count(arr, sub='e') print(res) Output[1 2 2] Explanation:"Berlin" -> one "e""Venice" -> two "e""New Delhi" -> two "e"Example 3: This example counts digits inside numeric strings. Python import numpy as np arr = np.array(['10101', '12345', '111']) res = np.char.count(arr, sub='1') print(res) Output[3 1 3] Explanation: Counts of "1" are 3, 1 and 3 for the respective strings. Create Quiz Comment J jana_sayantan Follow 0 Improve J jana_sayantan Follow 0 Improve Article Tags : Python Python-numpy Python numpy-String Operation Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like