Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.startswith

Tests a literal prefix or tuple of prefixes at the beginning of the given interval.

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

Syntax

s.startswith(prefix[, start[, end]])

Tests a literal prefix or tuple of prefixes at the beginning of the given interval.

The optional starting index moves the comparison point without requiring a sliced copy.

A tuple asks whether any literal prefix matches. In id:42 offsets zero through two belong to id:, so the second check for 42 starts at three. An omitted end reaches the end of the string.

assert "ab:cd".startswith(("cd", "xy"), 3, 5) and not "ab:cd".startswith("cd", 3, 4)

Examples

print("id:42".startswith(("id:", "key:")))
print("id:42".startswith("42", 3))

Result

True
True

A tuple asks whether any literal prefix matches. In id:42 offsets zero through two belong to id:, so the second check for 42 starts at three. An omitted end reaches the end of the string.

Sources