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
str.endswith
Returns True if the string ends with the given suffix, otherwise False. suffix may be a tuple of strings to test multiple candidates at once.
str.find
Returns the lowest index in the string where substring sub is found within the slice [start:end]. Returns -1 if the substring is not found, unlike index() which raises ValueError.
str.strip
Returns a copy of the string with leading and trailing whitespace removed. If chars is given, removes any leading/trailing characters that appear in the chars set (not a prefix/suffix — it is a character set).
str.rfind
Returns the highest index in the string where substring sub is found within [start:end]. Returns -1 if the substring is not found.
str.index
Like find(), but raises ValueError if the substring is not found. Use when the substring is expected to exist and its absence is an error.