String Methods

str.count()

Count non-overlapping occurrences of a substring within optional bounds. An empty substring matches each valid boundary, including the endpoints; for full text the result is len(text) + 1. Overlapping matches are not counted. The original string is unchanged.

Formal Syntax & Structure

python syntaxpython
text.count(sub[, start[, end]])

Parameters & Attributes

NameTypeDescription
substrSubstring to locate or count.
startint | NoneOptional inclusive start bound; negative values are relative to the end.
endint | NoneOptional exclusive end bound; defaults to the end.

Return Value

int

Number of non-overlapping matches, zero if absent.

Working Example

Example Snippetpython
print("aaaa".count("aa"))print("banana".count("ana"))print("سلام".count(""))
Python Reference