In this article we are going to learn python string count() method which is associated with python string data type.

Python string count method is used to count the number of occurrences of sub-string in a string. Basically it counts the number of overlapping occurrences of a sub-string. It searches the sub-string in a given string and returns how many times the sub-string is repeated.

String count method in python is useful to find out how many times a specific character or a word or a sentence is repeated in a complete string or in a portion of string.

String count method syntax and arguments:

string.count(substring, start, end)

For the execution of python count() method it needs sub-string as a mandatory argument. It also takes couple of optional parameters;

  • sub-string: This argument specifies the string which we want to count. This is mandatory argument which will be seared within the main input string.
  • start (Optional argument): This is the starting index of string from where we want to start the search.
  • end (Optional argument): This is ending index of string till which we want to search to be performed.

If we don’t specify the optional start and end argument then search will be performed on complete string.

Start and end arguments are helpful while dealing with big strings. By specifying the start and end argument we can limit the search to a specific portion of string. So instead of testing complete string every time we can narrow down the search length.

When we specify the start and end argument, It first perform string slicing operation on main string and then the search. The start and end argument will be considered as the starting and ending index for the string slicing operation.

Click here to learn more about string slicing and indexing.

Example 1: Count number of occurrences using python count method()

# Define a string.
FullString = "This is best python blog, isn't it?"
SubString = "is"

# Count number of occurrences of sub-string.
number_of_occurrences = FullString.count(SubString)

# Print the occurrence count.
print(number_of_occurrences)

#Output:
3

In example above we have not specified the start and end argument hence the search is performed on complete string (“This is best python blog, isn’t it?”). Sub-string (“is“) is repeated for 3 times in main input string.

Example 2: Count number of occurrences using python count method() along with specifying start and end argument.

# Define a string.
FullString = "This is best python blog, isn't it?"
SubString = "is"

# Count number of occurrences of sub-string.
number_of_occurrences = FullString.count(SubString, 4, 31)

# Print the occurrence count.
print(number_of_occurrences)

#Output:
2

In example above we have provided the start and end argument. Therefore the search will be performed only on portion of string starting from index 4 till index 31 (i.e “is best python blog, isn’t “). So the sub-string (“is”) is repeated only twice in this string portion.

String count operation using regular expression:

Other the python string count method we can count the occurrences of sub-string using regular expressions as well.

# Define a string.
FullString = "This is best python blog, isn't it?"
SubString = "is"

# Import the regular expression.
import re

# Count number of occurrences of sub-string.

number_of_occurences = re.findall(SubString, FullString)

# Print the occurrence count.
print(number_of_occurrences)

#Output:
3

So that was all about how we can count the number of occurrences of a sub-string using python string count method.

Continue Reading:

Share this Post

Leave a Comment

Your email address will not be published.