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.

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top