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
Syntax
str.endswith(suffix, start=0, end=len(s))
Parameters
| Parameter | Description |
|---|---|
suffix |
A string, or a tuple of strings to try. |
start, end optional |
Only check this slice. |
Return value
True or False.
Examples
print("report.pdf".endswith(".pdf"))
print("photo.JPG".lower().endswith((".jpg", ".png")))
Output
True True
url = "https://example.com/"
print(url.endswith("/"), url.endswith(".com"))
Output
True False
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.startswith()· Return True if the string starts with the given prefix. Pass a tuple to check several prefixes at once.str.removesuffix()· Return the string without the given suffix if it ends with it; otherwise return it unchanged.str.rfind()· Return the highest index where a substring is found, searching from the right, or -1 if it isn't found.- Python Strings · tutorial