Python startswith()

Learn how Python startswith() works with simple examples. Check if a string starts with a prefix, use start and end positions, match multiple prefixes with a tuple, and perform case-insensitive startswith checks.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

Python startswith()

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:

python
text = "Python Tutorial"
prefix = "py"

print(text.startswith(prefix))
print(text.casefold().startswith(prefix.casefold()))
Output

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))

  • prefix is required. It can be a string or a tuple of strings.
  • start is optional. It sets where checking begins.
  • end is optional. It limits the range (the end index is not included, like slicing).
  • Return value is True or False.

Basic Python startswith() example

python
language = "Python"
filename = "report_2026.txt"
url = "https://example.com"

print(language.startswith("Py"))
print(filename.startswith("report"))
print(url.startswith("https"))
Output

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.

python
text = "Python"

print(text.startswith("Py"))
print(text.startswith("py"))
Output

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.

python
text = "My name is Khan"
prefix = "my"

print(text.lower().startswith(prefix.lower()))
print(text.casefold().startswith(prefix.casefold()))
Output

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.

python
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)))
Output

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.

python
text = "123name456"

print(text.startswith("name"))
print(text.startswith("name", 3))
Output

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.

python
text = "123name456"

print(text.startswith("name", 3, 7))
print(text.startswith("name", 3, 6))
Output

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
python
text = "Learn Python"

print(text.startswith("Python"))
print("Python" in text)
Output

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.
python
text = "prefix_value"

print(text.startswith("prefix_"))
print(text.removeprefix("prefix_"))
Output

Use startswith() for validation. Use removeprefix() when you want the remaining string.


Practical examples of startswith()

python
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)
Output

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 in when the match must be at the beginning.
  • Using startswith() when you need regex pattern matching.
  • Expecting startswith() to return the matching prefix. It returns True or False.
  • Confusing start/end parameters with substring extraction.
  • Forgetting that strings are immutable.
  • Using upper() or lower() 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


Frequently Asked Questions

1. What does startswith() do in Python?

startswith() returns True if a string begins with the given prefix, otherwise False. It does not change the original string.

2. Is Python startswith() case-sensitive?

Yes by default. "Python".startswith("py") returns False because uppercase and lowercase letters are compared exactly.

3. How do you make startswith() case-insensitive?

Normalize both strings the same way, for example text.casefold().startswith(prefix.casefold()). Convert the prefix too, not only the main string.

4. Can startswith() check multiple prefixes at once?

Yes. Pass a tuple such as text.startswith(("http://", "https://")). A list does not work unless you convert it to a tuple.

5. What is the difference between startswith() and in?

startswith() checks the beginning of the string. in checks whether a substring appears anywhere.

6. What is the difference between startswith() and removeprefix()?

startswith() returns True or False. removeprefix() returns a new string with the prefix removed when it is present.
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …