Python endswith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

python
ok = filename.endswith(".py")
CallWhat 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
python
str.endswith(suffix, start=0, end=len(s)) → bool

Parameters

ParameterDescription
suffixRequired. Ending text, or a tuple of endings.
startOptional. Index where the check starts. Default 0.
endOptional. Exclusive end index of the check range.

Return Value

Return ValueDescription
boolTrue if the string ends with the suffix.

Python Examples

Example 1 — Basic endswith()

Check whether a filename ends with a specific extension.

python
filename = "document.txt"
is_text = filename.endswith(".txt")

print(f"File: {filename}")
print(f"Is text file: {is_text}")

How It Works

  1. Pick a filename. "document.txt" is the haystack.
  2. Pass the suffix. ".txt" is four characters long.
  3. Compare the ending. The last four characters match exactly.
  4. 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.

python
filename = "photo.jpg"
image_exts = (".jpg", ".jpeg", ".png", ".gif")

is_image = filename.endswith(image_exts)
print(f"{filename} is an image: {is_image}")

How It Works

  1. Group allowed endings. A tuple lists every valid extension.
  2. One method call. endswith tests each suffix.
  3. First match wins. ".jpg" matches → True.
  4. 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.

python
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]))

How It Works

  1. Case matters. ".PDF" ≠ ".pdf" → strict check fails.
  2. Normalize first. .lower() then check a lowercase suffix.
  3. Full string. "running fast" ends with "fast", not "ing".
  4. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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