startswith() answers a yes or no question: does the string begin with the given text? It returns True or False, and the test is case-sensitive.
You can pass a tuple of prefixes to ask "does it start with any of these?". Optional start and end check the prefix inside a slice instead of at the very beginning. The mirror method for the other end is endswith().
text.startswith() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Pass a prefix string, or a tuple of prefixes. start and end are optional.
python
ok = text.startswith(prefix)
ok = text.startswith((prefix1, prefix2))
ok = text.startswith(prefix, start, end)
Call
What it does
"python".startswith("py")
Returns True. The text begins with py.
"python".startswith(("java", "py"))
Returns True. With a tuple, one matching prefix is enough.
"python".startswith(["py"])
Raises TypeError. The prefix must be a string or a tuple, not a list.
Use endswith() to test the end of a string. To remove a known prefix, see removeprefix(). If you also need the position of a match, use find().
Library declarations
python
str.startswith(prefix[, start[, end]]) -> bool
# prefix: a str, or a tuple of str
# returns: True if the string (or the slice start:end) begins with prefix
# raises: TypeError if prefix is not a str or a tuple of str
Parameters
Parameter
Description
prefix
Required. A string, or a tuple of strings. The result is True if the text begins with any one of them.
start
Optional. Index where the test begins. Default 0.
end
Optional. Index where the tested slice stops, exclusive. Default is the end of the string.
Return Value
Return Value
Description
bool
True if the string, or the slice from start to end, begins with the prefix. Otherwise False. An empty prefix always gives True.
Python Examples
Example 1 — Check How a Web Address Starts
Test a URL for a scheme and see that the comparison is case-sensitive.
A matching start. The address begins with https, so the first line is True.
The whole prefix must match."http://" includes the colon and slashes, which this address does not start with.
Case matters."Python" starts with a capital P, so "py" does not match until the text is lowered.
Example 2 — Match Any of Several Prefixes
Pass a tuple to keep only the file names that start with one of two prefixes.
python
names = ["img_01.png", "doc_02.txt", "img_03.jpg", "tmp_04.png"]
wanted = [n for n in names if n.startswith(("img_", "doc_"))]
print(wanted)
print(len(names) - len(wanted))
Output
['img_01.png', 'doc_02.txt', 'img_03.jpg']
1
How It Works
A tuple of prefixes.startswith(("img_", "doc_")) is True when the name begins with either one.
Three pass the filter. The two img_ files and the doc_ file are kept.
One is dropped.tmp_04.png starts with neither prefix, so the difference in length is 1.
Example 3 — Test the Middle of a String
Give start and end to check a prefix inside a slice.
python
line = "ERROR: disk full"
print(line.startswith("disk", 7))
print(line.startswith("ERR", 0, 3))
print(line.startswith("ERROR", 0, 3))
print("python".startswith(""))
Output
True
True
False
True
How It Works
Start at index 7. The first seven characters are "ERROR: ", so the text from index 7 begins with disk.
A short window. With end set to 3, only "ERR" is examined, and it starts with ERR.
The window can be too small."ERROR" is longer than the three-character window, so the answer is False.
An empty prefix. Every string starts with the empty string, so the last line is True.
Frequently Asked Questions
It returns True if the string begins with the given prefix and False if it does not. The comparison is case-sensitive.
Yes. Pass a tuple, such as text.startswith(("http://", "https://")). The result is True if the text begins with any of them.
The prefix must be a string or a tuple of strings. Convert a list with tuple(my_list) before you pass it.
Lower both sides first, for example text.lower().startswith(prefix.lower()), or use casefold() for international text.
text.startswith("ab") is the same as text[:2] == "ab", but it reads better, avoids counting characters, and accepts a tuple.