Python String count() Method

Example

Return the number of times "apple" appears in the string:

txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)

Run Instance

Definition and Usage

The count() method returns the number of times the specified value appears in the string.

Syntax

string.count(value, start, end)

Parameter Value

Parameter Description
value Required. String. The string to be searched.
start Optional. Integer. The position to start the search. The default is 0.
end Optional. Integer. The position to end the search. The default is the end of the string.

More Examples

Example

Search from position 10 to 24:

txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple", 10, 24)
print(x)

Run Instance