str.startswith allows you to supply a tuple of strings to test for:

if link.lower().startswith(("js", "catalog", "script", "katalog")):

From the docs:

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

Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of prefixes to look for.

Below is a demonstration:

>>> "abcde".startswith(("xyz", "abc"))
True
>>> prefixes = ["xyz", "abc"]
>>> "abcde".startswith(tuple(prefixes)) # You must use a tuple though
True
>>>
Answer from user2555451 on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_startswith.asp
Python String startswith() Method
Remove List Duplicates Reverse ... Q&A Python Training ... The startswith() method returns True if the string starts with the specified value, otherwise False....
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ strings โ€บ .startswith()
Python | Strings | .startswith() | Codecademy
April 17, 2025 - The .startswith() method in Python checks whether a string begins with a specified value and returns True if it does. Otherwise, it returns False.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ startswith
Python String startswith()
Online Python Online JavaScript ... Online Rust Online Scala Online Dart Online R Online Ruby ... The startswith() method returns True if a string starts with the specified prefix(string)....
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ str โ€บ startswith.html
startswith โ€” Python Reference (The Right Way) 0.1 documentation
Returns a Boolean stating whether a string starts with the specified prefix. ... Required. The substring looked for. prefix can also be a tuple of prefixes to look for. ... Optional. Specifies beginning position for the search. ... Optional. Specifies ending position for the search.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_startswith.htm
Python String startswith() Method
The Python string method startswith() checks whether string starts with a given substring or not.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard library โ€บ str โ€บ startswith()
Python str startswith() - Check Prefix Presence
December 11, 2024 - Define a prefix you expect the string to start with. Use the startswith() method to verify the presence of this prefix.
Find elsewhere
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-string-startswith-method
Python String startswith() Method - Learn By Example
April 20, 2020 - The startswith() method returns True if the string starts with the specified prefix, otherwise returns False.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ stdtypes.html
Built-in Types โ€” Python 3.14.7 documentation
With optional start, test string beginning at that position. With optional end, stop comparing string at that position. ... >>> 'Python'.startswith('Py') True >>> 'a tuple of prefixes'.startswith(('at', 'a')) True >>> 'Python is amazing'.startswith('is', 7) True
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-pandas-series-str-startswith
Python | Pandas Series.str.startswith() - GeeksforGeeks
July 11, 2025 - Syntax: Series.str.startswith(pat, na=nan) Parameters: pat: String to be searched. (Regex are not accepted) na: Used to set what should be displayed if the value in series is NULL. Return type: Boolean series which is True where the value has the passed string in the start.
Top answer
1 of 3
34

There is technically no reason to accept other sequence types, no. The source code roughly does this:

if isinstance(prefix, tuple):
    for substring in prefix:
        if not isinstance(substring, str):
            raise TypeError(...)
        return tailmatch(...)
elif not isinstance(prefix, str):
    raise TypeError(...)
return tailmatch(...)

(where tailmatch(...) does the actual matching work).

So yes, any iterable would do for that for loop. But, all the other string test APIs (as well as isinstance() and issubclass()) that take multiple values also only accept tuples, and this tells you as a user of the API that it is safe to assume that the value won't be mutated. You can't mutate a tuple but the method could in theory mutate the list.

Also note that you usually test for a fixed number of prefixes or suffixes or classes (in the case of isinstance() and issubclass()); the implementation is not suited for a large number of elements. A tuple implies that you have a limited number of elements, while lists can be arbitrarily large.

Next, if any iterable or sequence type would be acceptable, then that would include strings; a single string is also a sequence. Should then a single string argument be treated as separate characters, or as a single prefix?

So in other words, it's a limitation to self-document that the sequence won't be mutated, is consistent with other APIs, it carries an implication of a limited number of items to test against, and removes ambiguity as to how a single string argument should be treated.

Note that this was brought up before on the Python Ideas list; see this thread; Guido van Rossum's main argument there is that you either special case for single strings or for only accepting a tuple. He picked the latter and doesn't see a need to change this.

2 of 3
22

This has already been suggested on Python-ideas a couple of years back see: str.startswith taking any iterator instead of just tuple and GvR had this to say:

The current behavior is intentional, and the ambiguity of strings themselves being iterables is the main reason. Since startswith() is almost always called with a literal or tuple of literals anyway, I see little need to extend the semantics.

In addition to that, there seemed to be no real motivation as to why to do this.

The current approach keeps things simple and fast, unicode_startswith (and endswith) check for a tuple argument and then for a string one. They then call tailmatch in the appropriate direction. This is, arguably, very easy to understand in its current state, even for strangers to C code.

Adding other cases will only lead to more bloated and complex code for little benefit while also requiring similar changes to any other parts of the unicode object.

๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ python string startswith: check if string starts with substring
Python String startswith: Check if String Starts With Substring โ€ข datagy
December 16, 2022 - The Python startswith() function checks whether or not a string starts with a substring and returns a boolean value. The function will return True if the string starts with the provided substrings and False otherwise.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-string-startswith
Python - String startswith() - GeeksforGeeks
April 29, 2025 - startswith() method in Python checks whether a given string starts with a specific prefix.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ string โ€บ python-string-startswith-function
Python String startswith() Function - AskPython
August 6, 2022 - String in Python has built-in functions for almost every action to be performed on a string. Python String startswith() function checks for the specific prefix in a string and returns True else False.
๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ Python โ€บ en โ€บ String-Methods-startswith
Python String startswith(), Definition, Syntax, Parameters, Examples | jobtensor
string.startswith(value, start, end) testStr = "Welcome to the Python tutorials." result = testStr.startswith("Welcome") print(result) # using the start and end parameter testStr = "Welcome to the Python tutorials." result = testStr.startswith("the", 11, 26) print(result) Previous splitlines() Next strip() Python Tutorial ยท
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Strange behavior when using String.startswith() with an empty string and a big start - Python Help - Discussions on Python.org
September 29, 2021 - When using 'test'.startswith(''), it came out as True, I was under the impression that startswith() was doing something like this: string[start:end] == value, but whenever start is equal or greater than the length of the string it results in False. So, I thought that values that would result in out of bounds in other languages would all return False, but 'test'.startswith('', 0, 99), 'test'.startswith('', -99, 99) and 'test'.startswith('', -99, -99) return True.
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python startswith and endswith: step-by-step guide
Python Startswith and Endswith: Step-By-Step Guide | Career Karma
December 1, 2023 - Because each character has its own index number, we can manipulate strings based on where each letter is located. The startswith() string method checks whether a string starts with a particular substring.