The startswith() method checks whether a string begins with a given prefix. It returns True or False and does not modify the original string.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Quick answer: check a string prefix in Python
Use text.startswith(prefix) to check if a string starts with a prefix. For case-insensitive checks, normalize both values:
text = "Python Tutorial"
prefix = "py"
print(text.startswith(prefix))
print(text.casefold().startswith(prefix.casefold()))The first call returns False because startswith() is case-sensitive. The second returns True after both strings use casefold().
Quick reference
| Task | Use |
|---|---|
| Check if string starts with prefix | text.startswith("Py") |
| Case-insensitive startswith | text.casefold().startswith("py".casefold()) |
| Check multiple prefixes | text.startswith(("http://", "https://")) |
| Start checking from index | text.startswith("name", 3) |
| Check within start and end range | text.startswith("name", 3, 7) |
| Check opposite ending | text.endswith(".txt") |
| Remove prefix if present | text.removeprefix("prefix") |
| Check prefix with regex pattern | re.match(pattern, text) |
Python startswith() syntax
Syntax: string.startswith(prefix, start=0, end=len(string))
prefixis required. It can be a string or a tuple of strings.startis optional. It sets where checking begins.endis optional. It limits the range (the end index is not included, like slicing).- Return value is
TrueorFalse.
Basic Python startswith() example
language = "Python"
filename = "report_2026.txt"
url = "https://example.com"
print(language.startswith("Py"))
print(filename.startswith("report"))
print(url.startswith("https"))All three checks return True when the prefix matches the beginning of the string.
Python startswith() is case-sensitive
startswith() treats uppercase and lowercase letters differently.
text = "Python"
print(text.startswith("Py"))
print(text.startswith("py"))The first returns True. The second returns False. That is expected behavior, not a bug.
Python startswith() ignore case
Use lower() for simple ASCII-style comparisons. Use casefold() for stronger case-insensitive text comparison. Normalize both the main string and the prefix.
text = "My name is Khan"
prefix = "my"
print(text.lower().startswith(prefix.lower()))
print(text.casefold().startswith(prefix.casefold()))Do not convert only the main string when the prefix comes from user input or a variable. Convert both sides the same way.
startswith() with multiple prefixes
Pass a tuple of prefixes to test several options at once. startswith() does not accept a list directly—convert a list to a tuple if needed.
url = "https://example.com"
scheme = "ftp://archive.local"
print(url.startswith(("http://", "https://")))
print(scheme.startswith(("http://", "https://")))
prefixes = ["ERROR", "WARN"]
line = "WARN: disk space low"
print(line.startswith(tuple(prefixes)))Useful for URL schemes, command prefixes, log levels, and allowed value checks.
startswith() with start parameter
start tells Python where to begin checking. It does not slice the string permanently—it only changes the test position.
text = "123name456"
print(text.startswith("name"))
print(text.startswith("name", 3))The first check is False. The second is True because checking begins at index 3.
startswith() with start and end parameters
end limits the comparison range. The end index is not included, like normal slicing.
text = "123name456"
print(text.startswith("name", 3, 7))
print(text.startswith("name", 3, 6))The slice text[3:7] is "name", so the first call returns True. The slice text[3:6] is "nam", so the second returns False.
startswith() vs in operator
| Check | Meaning |
|---|---|
text.startswith("Py") |
Prefix must be at the beginning |
"Py" in text |
Substring can appear anywhere |
text = "Learn Python"
print(text.startswith("Python"))
print("Python" in text)Use startswith() when position matters. Use in when the substring can appear anywhere. See check if a string contains a substring for more membership checks beyond prefixes.
startswith() vs find()
| Method | Return type | Best for |
|---|---|---|
startswith() |
True or False |
Check prefix |
find() |
Index or -1 |
Find substring position |
Use startswith() when you only need a yes-or-no prefix check. Use find() when you need the position.
startswith() vs removeprefix()
startswith()checks whether a prefix exists.removeprefix()removes the prefix if present and returns a new string.
text = "prefix_value"
print(text.startswith("prefix_"))
print(text.removeprefix("prefix_"))Use startswith() for validation. Use removeprefix() when you want the remaining string.
Practical examples of startswith()
url = "http://internal.local/api"
filename = "tmp_report.csv"
log_line = "ERROR: connection refused"
command = "git status"
print(url.startswith(("http://", "https://")))
print(filename.startswith("tmp_"))
print(log_line.startswith(("ERROR", "WARN")))
print(command.startswith("git"))
lines = ["INFO: ok", "ERROR: fail", "WARN: slow", "INFO: done"]
errors = [line for line in lines if line.startswith("ERROR")]
print(errors)These patterns fit URLs, filenames, logs, commands, and filtering a list by prefix.
Common mistakes with startswith()
- Forgetting that
startswith()is case-sensitive. - Converting only the string but not the prefix for ignore-case checks.
- Passing a list instead of a tuple for multiple prefixes.
- Using
inwhen the match must be at the beginning. - Using
startswith()when you need regex pattern matching. - Expecting
startswith()to return the matching prefix. It returnsTrueorFalse. - Confusing
start/endparameters with substring extraction. - Forgetting that strings are immutable.
- Using
upper()orlower()inconsistently across input values.
Summary
startswith() returns True if a string begins with a prefix. It is case-sensitive by default. Use lower() or casefold() on both the string and prefix for case-insensitive checks. Pass a tuple to check multiple prefixes. Use start and end when you need to test a specific range. For suffix checks, use endswith() or compare with related string methods such as removeprefix().
References
- Python
str.startswith()documentation - Python
str.endswith()documentation - Python
str.removeprefix()documentation - Python
str.casefold()documentation

