🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › startsWith
String.prototype.startsWith() - JavaScript - MDN Web Docs
The startsWith() method of String values determines whether this string begins with the characters of a specified string, returning true or false as appropriate.
🌐
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....
🌐
Vultr Docs
docs.vultr.com › python › standard library › str › startswith()
Python str startswith() - Check Prefix Presence
December 11, 2024 - The startswith() method in Python is a string method that checks whether a given string starts with a specified prefix. This check is case-sensitive and plays a critical role in data validation, parsing, and filtering tasks when dealing with ...
🌐
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()
text = "Python programming is easy." # start parameter: 7 # 'programming is easy.' string is searched · result = text.startswith('programming is', 7) print(result) # start: 7, end: 18 # 'programming' string is searched
🌐
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. This method accepts a prefix string that you want to search for and is invoked on a string object.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.startswith
String.StartsWith Method (System) | Microsoft Learn
The following example defines a StripStartTags method that uses the StartsWith(String) method to remove HTML start tags from the beginning of a string. Note that the StripStartTags method is called recursively to ensure that multiple HTML start tags at the beginning of the line are removed.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java string › java string.startswith()
Java.String.startsWith() | Baeldung
April 11, 2025 - The method startsWith() is a convenience method that checks whether a String starts with another String.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-startswith-method-example
Java String startsWith() Method with examples
September 16, 2022 - boolean startsWith(String str, index fromIndex): It returns true if the String begins with str, it starts looking from the specified index “fromIndex”.
🌐
MathWorks
mathworks.com › matlab › language fundamentals › data types › characters and strings
startsWith - Determine if strings start with pattern - MATLAB
TF = startsWith(str,pat) returns 1 (true) if str starts with the specified pattern, and returns 0 (false) otherwise.
🌐
W3Schools
w3schools.com › java › ref_string_startswith.asp
Java String startsWith() Method
String myStr = "Hello"; ... Yourself » · Share Link Copied · The startsWith() method checks whether a string starts with the specified character(s)....
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.