Qu

count

Builtin index · Collections & strings

result = count(s, sub)

Input: s, sub · Output: result

What it is

s and sub are Strs.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = count(s, sub)
s Strnot documented yet
sub Strnot documented yet
s —non-overlapping so it agrees with replace
Returns a number: how many non-overlapping times sub occurs in s (non-overlapping so it agrees with replace).

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

print("-" * 40)                                  # "----------------------------------------"

print("hello".substr(0, 2).replace("e", "i"))    # "hi"

print(pad_left(str(7), 3, "0"))                  # "007"

print(proper("ada lovelace"))                    # "Ada Lovelace"

print(count("banana", "a"))                      # 3

output
----------------------------------------
hi
007
Ada Lovelace
3

from collections-strings

How it works

count(s, sub) — non-overlapping occurrences. Non-overlapping is the choice every language makes here and the one that makes count agree with replace: "aaa" contains two overlapping "aa" but replace(s, "aa", "") removes one.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.