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
| Name | Type | Description |
|---|---|---|
| sub | str | Substring to locate or count. |
| start | int | None | Optional inclusive start bound; negative values are relative to the end. |
| end | int | None | Optional 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(""))