Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.count

Counts non-overlapping occurrences of a substring within the selected range.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.count(sub[, start[, end]])

Counts non-overlapping occurrences of a substring within the selected range.

The empty substring matches between characters and at both ends.

The next search starts after the entire match. Four letters a therefore contain two non-overlapping aa matches. Three characters have four boundaries where the empty substring matches. The optional end excludes the character at that position, but an empty match can occur at that boundary.

assert "abc".count("", 1, 2) == 2

Examples

print("aaaa".count("aa"))
print("abc".count(""))

Result

2
4

The next search starts after the entire match. Four letters a therefore contain two non-overlapping aa matches. Three characters have four boundaries where the empty substring matches. The optional end excludes the character at that position, but an empty match can occur at that boundary.

Sources