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

ParameterDescription
prefix A string, or a tuple of strings to try.
start, end optional Only check this slice.

Return value

True or False.

Examples

Check prefixesTry it yourself
print("https://example.com".startswith("https://"))
print("ftp://x".startswith(("http://", "https://")))

Output

True
False
Skip comment linesTry it yourself
lines = ["# comment", "code()", "  # indented comment"]
print([l for l in lines if not l.lstrip().startswith("#")])

Output

['code()']

Version history

VersionChange
Python 3.0Added