Python String startswith() Method
str.startswith(prefix) returns True if the string begins with prefix and False otherwise. Pass a tuple of prefixes to test several at once.Use startswith() for questions like "is this a web address?", "does this file name begin with img_?" or "is this line a comment?". It is clearer and safer than slicing by hand, because you do not have to count characters. Its mirror image is endswith(), and both return a boolean you can drop straight into an if statement.
| Syntax | text.startswith(prefix, start, end) |
|---|---|
| Parameters | prefix: a string or a tuple of strings; start, end (optional): test text[start:end] |
| Returns | True or False |
| Case sensitive | Yes |
| Empty prefix | Always True |
How str.startswith() works
Python compares the first characters of the string with the prefix. The prefix may be a single character or a whole phrase.
file = "report_2024.csv"
print(file.startswith("report"))
print(file.startswith("Report"))
print(file.startswith(""))The second line is False because of the capital R. The empty prefix is always True, which matters if a prefix comes from user input.
Checking several prefixes with a tuple
To test several alternatives, pass a tuple (not a list) as the prefix. The result is True if the string starts with any item. This replaces a chain of or conditions.
url = "https://example.com"
print(url.startswith(("http://", "https://")))
print("ftp://x.org".startswith(("http://", "https://")))A list raises TypeError; a tuple is required. See Python tuples for why.
Using the start and end arguments
With start, the test begins at that index instead of at the beginning, so "hello".startswith("ll", 2) is True. This is handy when parsing a fixed-width record and you want to inspect one field without slicing out a copy.
startswith() vs slicing vs in
| Tool | Example | What it tells you |
|---|---|---|
startswith() | s.startswith("ab") | Does the string begin with this text? |
| Slicing | s[:2] == "ab" | Same answer, but you must count the length yourself |
in | "ab" in s | Is the text anywhere in the string? |
endswith() | s.endswith("ab") | Does the string finish with this text? |
removeprefix() | s.removeprefix("ab") | Returns the string without the prefix (Python 3.9+) |
Examples you can run
Press Run, then edit the code and run it again.
Filter file names by prefix
A list comprehension with startswith() keeps only the files you want.
files = ["img_01.png", "notes.txt", "img_02.png", "data.csv"]
images = [f for f in files if f.startswith("img_")]
print(images)Expected output['img_01.png', 'img_02.png']
Test several prefixes at once
A tuple of prefixes checks all of them in one call.
links = ["http://a.com", "mailto:b@c.com", "https://d.org", "/home"]
for link in links:
print(link, link.startswith(("http://", "https://")))Expected outputhttp://a.com True
mailto:b@c.com False
https://d.org True
/home False
Ignore case and surrounding spaces
Clean the text first, then test. This handles " Hello" and "HELLO" the same way.
line = " # TODO: fix login"
cleaned = line.strip().lower()
print(cleaned.startswith("# todo"))Expected outputTrue
Look at a part of the string with start
The second argument shifts where the comparison starts.
code = "IN-2024-0042"
print(code.startswith("2024", 3))
print(code.startswith("2024"))Expected outputTrue
False
Common mistakes
Passing a list of prefixes
✗ Wrong
print("hello".startswith(["he", "ha"]))✓ Right
print("hello".startswith(("he", "ha")))Use a tuple, or convert the list with tuple(prefixes).
Forgetting that it is case sensitive
✗ Wrong
print("Python".startswith("py"))✓ Right
print("Python".lower().startswith("py"))Normalise with lower() on the text first.
Ignoring leading whitespace
✗ Wrong
print(" yes".startswith("yes"))✓ Right
print(" yes".strip().startswith("yes"))Call strip() first when the text may come from a user or a file.
Python String startswith() Method: questions people ask
How do I check if a string starts with a letter or word in Python?
Use text.startswith("word"). It returns True when the string begins with that text.
Can startswith() check multiple prefixes?
Yes. Pass a tuple, such as text.startswith(("a", "b")); it returns True if any prefix matches.
Is startswith() faster than slicing?
They are similar for short strings, but startswith() is clearer and avoids creating a temporary slice, so prefer it.
Keep learning
Step-by-step lessons:
Python StringsPython String Methods
Related:
Python String endswith() MethodPython String find() MethodPython String strip() MethodHow to Check if a String Contains a Substring in Python
More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() Method See all →