Python String count() Method

You are Here:

Python String count()

The count() method returns the number of times a given value occurs in a string.

By default, the counting begins from the start of the string till the end.

Example

Python Compiler
txt = "wikimass is a mass website" x = txt.count("mass") print(x)

Output

2

Syntax

txt.count(value, startIndex, endIndex)

Parameter Values

ValueTypeExplanation
valueRequiredSpecifies a string to value to search for.
startIndexRequiredSpecifies the starting position to start searching.
Default value is 0.
endIndexRequiredSpecifies the ending position to end searching.
Default value is the length of the string.

Return Value

ValueExplanation
NumberReturns the number of times a given value occurs in a string.

More Example

In the following example, we start searching for "mass" from the index number 10 to 25.

Example

Python Compiler
txt = "wikimass is a mass website" x = txt.count("mass", 10, 25) print(x)

Output

1

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.

We are working to cover every Single Concept in Python.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author