search

str.startswith

Returns True if the string starts with the given prefix, otherwise False. prefix may also be a tuple of strings to test against multiple candidates.

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

Parameters

Parameter Purpose
prefix string or tuple of strings to test
start position to start testing (default 0)
end position to stop testing (default len(str))

Examples

>>> 'hello.py'.startswith('hello')
True

simple prefix match

>>> 'image.png'.startswith(('http://', 'https://'))
False

tuple lets you test multiple prefixes at once

>>> 'hello world'.startswith('world', 6)
True

start argument shifts the comparison window

>>> 'ABC'.startswith('abc')
False

case-sensitive — lowercase first for case-insensitive checks

Gotcha

Case-sensitive; call .lower() or .casefold() first for case-insensitive matches. Since Python 3.9, prefer str.removeprefix() when you also want to strip the prefix.

Related methods

← All Python string methods · Python built-ins