Python String count() Method

Last Updated:

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)

Syntax

txt.count(value, startIndex, endIndex)

Parameter Values

ValueTypeExplanation
value Required Specifies a string to value to search for.
startIndex Required Specifies the starting position to start searching.
Default value is 0.
endIndex Required Specifies the ending position to end searching.
Default value is the length of the string.

Return Value

ValueExplanation
Number Returns 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)

Share this Page

Meet the Author