String Methods

str.startswith()

Test whether the bounded text begins with the specified prefix. Accept a string or a tuple of candidate strings, testing until a match is found. Matching is exact and case-sensitive, not a regular expression. Empty candidates match valid boundaries; a tuple with no candidates fails. The original string is unchanged.

Formal Syntax & Structure

python syntaxpython
text.startswith(prefix[, start[, end]])

Parameters & Attributes

NameTypeDescription
prefixstr | tuple[str, ...]One literal candidate or tuple of alternatives.
startint | NoneOptional inclusive start bound; negative values are relative to the end.
endint | NoneOptional exclusive end bound; defaults to the end.

Return Value

bool

True if any candidate matches the requested boundary; otherwise False.

Working Example

Example Snippetpython
filename = "api.json"print(filename.startswith('api'))print(filename.startswith(('api', "other")))print(filename.startswith('.json'))
Python Reference