Python startswith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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

ParameterDescription
prefixRequired. A string, or a tuple of strings. The result is True if the text begins with any one of them.
startOptional. Index where the test begins. Default 0.
endOptional. Index where the tested slice stops, exclusive. Default is the end of the string.

Return Value

Return ValueDescription
boolTrue 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.

python
url = "https://codetofun.com"

print(url.startswith("https"))
print(url.startswith("http://"))
print("Python".startswith("py"))
print("Python".lower().startswith("py"))

How It Works

  1. A matching start. The address begins with https, so the first line is True.
  2. The whole prefix must match. "http://" includes the colon and slashes, which this address does not start with.
  3. 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))

How It Works

  1. A tuple of prefixes. startswith(("img_", "doc_")) is True when the name begins with either one.
  2. Three pass the filter. The two img_ files and the doc_ file are kept.
  3. 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(""))

How It Works

  1. Start at index 7. The first seven characters are "ERROR: ", so the text from index 7 begins with disk.
  2. A short window. With end set to 3, only "ERR" is examined, and it starts with ERR.
  3. The window can be too small. "ERROR" is longer than the three-character window, so the answer is False.
  4. 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.

Next: strip()

Return a copy with spaces, or chosen characters, removed from both ends.

strip() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful