Python String count() function

In this article, we will discuss how to use the count() function of string in python.

In python, the string class provides a function count() to get the occurrence count of a sub-string in a string or a portion of string.

Syntax of str.count()

str.count(sub_string, start, end)

Arguments:

  • sub_string: A string or a character, whose occurrence in string has to be counted.
  • start: (Optional) The starting index position of the string, from where count() function will start looking for the occurrence of sub_string. Default value is 0 i.e. start of string.
  • end: (Optional) The ending index position of the string, till where count() function will look for the occurrence of sub_string. Default value is the end of string.

Returns:

It returns the occurrence count of given sub-string or character in the complete string or in specified portion of the string.

Advertisements

Important Point: As strings are immutable in python, therefor it returns a new string object.

Let’s understand this with some examples,

Example 1: Count occurrences of a sub-string in the string using count()

sample_str = 'This is a sample string and'

# Count occurrences of a sub-string  in the string

sub_str = 'is'
occurrences = sample_str.count(sub_str)

print('Total occurrences :', occurrences)

Output:

Total occurrences : 2

As sub-string ‘is’ occurs at two different places in the string, therefore it returned the value 2.

Example 2: Count occurrences of a character in the string using count()

character = 's'
# Count occurrences of a character in the string
occurrences = sample_str.count(character)

print('Total occurrences :', occurrences)

Output:

Total occurrences : 4

As character ‘s’ occurs at four different places in the string, therefore it returned the value 4.

Example 3: Count occurrences of a sub-string in a portion of string (start to end) using count()

Let’s count the the occurrences of sub-string ‘is’ in first 5 characters of the string i.e. from start postion 0 to end position 5,

sub_str = 'is'
start = 0
end = 5

# Count occurrences of a sub-string  in first 5 characters of string
occurrences = sample_str.count(sub_str, start, end)
print('Total occurrences :', occurrences)

Output

Total occurrences : 1

As sub-string ‘is’ occurs only once in the first 5 characters of the string, therefore it returned the value 1.

Example 4: Count occurrences of a sub-string in a portion of string (from index 5 to 15)

Let’s count the the occurrences of sub-string ‘is’ from index postion 5 to index position 15,

sub_str = 'is'
start = 5
end = 15

# Count occurrences of a sub-string in a range i.e. from 5th to 15th chatacter in the string
occurrences = sample_str.count(sub_str, start, end)
print('Total occurrences :', occurrences)

Output

Total occurrences : 1

As sub-string ‘is’ occurs only once in the specified portion of the string, therefore it returned the value 1.

Example 5: Count occurrences of a sub-string that doesn’t exist in a string

sub_str = 'hello'

# Count occurrences of a sub-string that doesn't exist in a string
occurrences = sample_str.count(sub_str)

print('Total occurrences :', occurrences)

Output

Total occurrences : 0

As sub-string ‘is’ doesn’t exist in the string, therefore it returned the value 0.

Summary:

Today, we learned how to use the count() method of python string.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top