Python String count(): Return the Times Substring in String – Python Tutorial

By | August 28, 2019

Python string.count() function can return how many times of substring in a string. In this tutorial, we will write some examples to illustrate you how to use this function.

The syntax of count() method is:

string.count(substring, start=..., end=...)

Here is some examples:

str = 'https://www.tutorialexample.com/'

substring = '/'
count = str.count(substring)
print(count)

The count of ‘/‘ is: 3

count = str.count(substring, 7)
print(count)

The count of ‘/‘is: 2

From the result, we can find: start is contained (/www.tutorialexample.com/)

count = str.count(substring, 7, len(str)-1)
print(count)

The count of ‘/‘ is: 1

From the result, we can find: end is not contained (/www.tutorialexample.com).

Leave a Reply