Python Function Reference

str.startswith

Check whether the string starts with the given prefix.

Python 3.13
str.startswith(prefix[, start[, end]])
  • [ ]optional part
prefix
The expected beginning of the string; with a tuple, any of the options matches.
str | tuple
start
The index where the check starts, 0 by default.
intOptional
end
The index before which the check stops, the end of the string by default.
intOptional

Return value

True or False.

Examples

Checking the start of a string

Python 3.13
print('hello'.startswith('he'))
True

Checking several options

Python 3.13
print('https://example.com'.startswith(('http://', 'https://')))
True

Details

Pass several options as a tuple: a list raises TypeError. The check is case-sensitive; to ignore case, convert the string to lowercase first. An empty prefix matches any string.

See also

Where to learn