In python, the string function count() is used to count the frequency of a substring within a given string. That it, the count()
function returns the number of times a substring occurs inside a string. In this tutorial, we’ll look at its syntax and use-cases along with some examples.
Before we proceed, here’s a quick refresher on python strings – Strings are immutable sequences of unicode characters used to handle textual data. Strings implement all common sequence operations (example, iterations, membership tests, etc). For more, check out our guide on strings and other fundamentals of python.
The count() function
As mentioned above, the string function count()
in python returns the number of non-overlapping occurrences of a substring in a string. It also takes optional start and end parameters. The following is its syntax:
sample_string.count(sub, start, end)
Here, sample_string is the string in which you want to find the frequency of sub.
Parameters:
- sub: The substring to count.
- start (optional): The starting position in the string from where the substring is to be counted.
- end (optional): The ending position in the string till where the substring is to be counted.
Returns:
The count()
function returns the integer count of the number of non-overlapping occurrences of the substring in the range [start:end]
of the string.
Examples
Example 1: Count the occurrence of substring without optional parameters
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# count without optional parameters
s1 = "to code or not to code"
s2 = "Do not say codecodecodecode say code"
s3 = "Python is fun"
# count the occurrence of 'code'
c1 = s1.count("code")
c2 = s2.count("code")
c3 = s3.count("code")
# print
print("Count of 'code' in s1:", c1)
print("Count of 'code' in s2:", c2)
print("Count of 'code' in s3:", c3)
Output:
Count of 'code' in s1: 2
Count of 'code' in s2: 5
Count of 'code' in s3: 0
In the above example, the python string function count() is used to count the occurrence of the substring 'code'
in strings s1
, s2
, and s3
. It returns the count of the non-overlapping occurrences of the substring as integers: 2
, 5
, and 0
respectively.
Example 2: Count the occurrence of substring with optional parameters
# count with optional parameters
s = "to code or not to code"
# print the count
print(s.count("code"))
print(s.count("code",3))
print(s.count("code",4))
print(s.count("code",4,22))
print(s.count("code",4,21))
print(s.count("code",18,22))
print(s.count("code",19,22))
Output:
2
2
1
1
0
1
0
In the above example, the count()
function is used along with the start and/or the end optional parameters. To better understand the use of start and end parameters imagine them as the indices to slice a string, like s[start:end]
and then using the count() function to get the frequency of the substring.
For more, on python string functions, refer to the python docs.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.