Python 3 reference · String Methods
Python String startswith() Method
Return True if the string starts with the given prefix. Pass a tuple to check several prefixes at once.
Python 3 Since 3.0
Syntax
str.startswith(prefix, start=0, end=len(s))
Parameters
| Parameter | Description |
|---|---|
prefix |
A string, or a tuple of strings to try. |
start, end optional |
Only check this slice. |
Return value
True or False.
Examples
print("https://example.com".startswith("https://"))
print("ftp://x".startswith(("http://", "https://")))
Output
True False
lines = ["# comment", "code()", " # indented comment"]
print([l for l in lines if not l.lstrip().startswith("#")])
Output
['code()']
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.endswith()· Return True if the string ends with the given suffix. Pass a tuple to check several suffixes at once.str.removeprefix()· Return the string without the given prefix if it starts with it; otherwise return it unchanged.str.find()· Return the lowest index where a substring is found, or -1 if it isn't. Optional start and end limit the search.- Python Strings · tutorial