Python endswith() Method

Definition
In Python, endswith() returns True if a string ends with a given suffix, otherwise False.
suffix is required. It can be a string or a tuple of strings. Optional start and end limit the check to a slice.
Syntax
Call endswith with a suffix. Start with this line.
ok = filename.endswith(".py")
| Call | What it does |
|---|
"Main.py".endswith(".py") | Returns True. |
"a.png".endswith((".jpg", ".png")) | True if any suffix matches. |
The check is case-sensitive. Pass a tuple—not a list—for multiple suffixes.
Library declarations
str.endswith(suffix, start=0, end=len(s)) → bool
Parameters
| Parameter | Description |
|---|
| suffix | Required. Ending text, or a tuple of endings. |
| start | Optional. Index where the check starts. Default 0. |
| end | Optional. Exclusive end index of the check range. |
Return Value
| Return Value | Description |
|---|
| bool | True if the string ends with the suffix. |
Python Examples
Example 1 — Basic endswith()
Check whether a filename ends with a specific extension.
filename = "document.txt"
is_text = filename.endswith(".txt")
print(f"File: {filename}")
print(f"Is text file: {is_text}")
File: document.txt
Is text file: True
How It Works
- Pick a filename.
"document.txt" is the haystack. - Pass the suffix.
".txt" is four characters long. - Compare the ending. The last four characters match exactly.
- Get a bool.
True—the original string is unchanged.
Example 2 — Multiple Suffixes (Tuple)
Accept any image extension in one call—cleaner than chained or.
filename = "photo.jpg"
image_exts = (".jpg", ".jpeg", ".png", ".gif")
is_image = filename.endswith(image_exts)
print(f"{filename} is an image: {is_image}")
photo.jpg is an image: True
How It Works
- Group allowed endings. A tuple lists every valid extension.
- One method call.
endswith tests each suffix. - First match wins.
".jpg" matches → True. - No match → False. e.g.
"notes.docx" would fail all four.
Example 3 — Case Sensitivity and Slices
Extensions are case-sensitive; optional start/end check a slice.
filename = "Report.PDF"
text = "running fast"
print("Strict: ", filename.endswith(".pdf"))
print("Insensitive:", filename.lower().endswith(".pdf"))
print("Full ends: ", text.endswith("ing"))
print("Slice 0–7: ", text.endswith("ing", 0, 7), repr(text[0:7]))
Strict: False
Insensitive: True
Full ends: False
Slice 0–7: True 'running'
How It Works
- Case matters.
".PDF" ≠ ".pdf" → strict check fails. - Normalize first.
.lower() then check a lowercase suffix. - Full string.
"running fast" ends with "fast", not "ing". - Slice window.
text[0:7] is "running", which does end with "ing".
Frequently Asked Questions
It returns True if the string ends with the given suffix.
Yes. Pass a tuple such as (".jpg", ".png").
Yes. Lowercase the string when extensions may vary in case.
endswith() checks the end. startswith() checks the beginning.
No. It only returns a boolean.
Next: expandtabs()
Turn tab characters into spaces with a chosen tab size.
expandtabs() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful