String method

str.startswith()

Returns True when a string begins with the text you give, or with any one of several options passed as a tuple.

BeginnerEvery example below runs in your browser. Change the code and press Run.

str.startswith(prefix[, start[, end]])

startswith() answers a yes/no question: does this string begin with this text? It returns True or False.

Use it to filter file names, check that a web address begins with https://, or spot lines that start with a comment mark. It reads more clearly than slicing (text[:5] == "https") and you do not have to count characters.

You can pass a tuple of options to test several beginnings at once: url.startswith(("http://", "https://")). A list does not work there, only a tuple. The check is case-sensitive.

Parameters

ParameterWhat it is
prefix
required
The text the string should begin with, or a tuple of several texts (any one of them is enough).
start
optional, default 0
Check from this position instead of from the beginning.
end
optional, default len(str)
Stop checking at this position.

Return value

True if the string begins with prefix (or one of the tuple's items), otherwise False.

Examples

1A simple check

Pythoneditable
filename = "report_2026.pdf"
print(filename.startswith("report"))
print(filename.startswith("invoice"))
Output
True
False

2Several options with a tuple

Pythoneditable
links = ["https://example.com", "ftp://files.example.com", "http://old.example.com"]
for link in links:
    if link.startswith(("http://", "https://")):
        print("web page:", link)
    else:
        print("other:", link)
Output
web page: https://example.com
other: ftp://files.example.com
web page: http://old.example.com

3Filter a list of files

Pythoneditable
files = ["img_001.jpg", "notes.txt", "img_002.jpg", "todo.txt"]
photos = [f for f in files if f.startswith("img_")]
print(photos)
Output
['img_001.jpg', 'img_002.jpg']

4Skip comment lines in a settings file

Pythoneditable
with open("settings.txt") as f:
    for line in f:
        line = line.strip()
        if line.startswith("#") or not line:
            continue
        print(line)
Output
theme = dark
size = 14

5Ignore upper and lower case

Pythoneditable
answer = "Yes, please"
print(answer.startswith("yes"))
print(answer.lower().startswith("yes"))
Output
False
True

Common mistakes

Passing a list instead of a tuple

Several options must be given as a tuple. A list raises TypeError.

Pythoneditable
name = "data.csv"
try:
    print(name.startswith(["data", "info"]))
except TypeError:
    print("TypeError: use a tuple, not a list")
print(name.startswith(("data", "info")))
Output
TypeError: use a tuple, not a list
True

Questions people ask

How do I check if a string starts with a number?

Check the first character: text[:1].isdigit(). Using [:1] instead of [0] avoids an error on an empty string.

How do I check several prefixes stored in a list?

Convert the list: text.startswith(tuple(prefixes)).

How do I remove the prefix after checking it?

Use str.removeprefix(): it removes the prefix only if it is there, so you often do not need the check at all.

Spotted a mistake on this page? Tell us