Python 3 reference · String Methods

Python String endswith() Method

Return True if the string ends with the given suffix. Pass a tuple to check several suffixes at once.

Python 3 Since 3.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

str.endswith(suffix, start=0, end=len(s))

Parameters

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

Return value

True or False.

Examples

Check file extensionsTry it yourself
print("report.pdf".endswith(".pdf"))
print("photo.JPG".lower().endswith((".jpg", ".png")))

Output

True
True
Trailing charactersTry it yourself
url = "https://example.com/"
print(url.endswith("/"), url.endswith(".com"))

Output

True False

Version history

VersionChange
Python 3.0Added

In other languages

Was this page helpful?