🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython String endswith() Function

A text value may need to match one expected ending or one of several alternatives. Python’s str.endswith() method checks whether a string ends with the supplied suffix and returns True or False.
This article explains how to pass several suffixes as a tuple and how start and end limit the checked range.
TL;DR
Call str.endswith(suffix) to test whether text ends with a suffix, or pass a tuple to test several alternatives.
It returns True or False and compares case-sensitively.
Optional start and end positions restrict the slice being checked.
Use a tuple, not separate suffix arguments, for multiple choices.
What is Python str.endswith()?
str.endswith() is a string method that returns a Boolean indicating whether a string ends with the supplied text. The suffix is the text at the right-hand edge, such as .csv in report.csv.
The method takes one required suffix argument, which can be a string or a tuple. Optional start and end positions limit the part Python checks, and end is excluded. With no positions, Python checks the full string.
The check is case-sensitive, so .CSV does not match .csv. The method checks text as written. It does not open a file or establish that the named file has a particular format.
The optional positions are integers, not extra suffixes. start marks where the check begins, while end marks the boundary just after the checked range. Python follows slice conventions, so a negative position counts backward from the end.
Choose boundaries from the original string rather than calculating a new substring first. A tuple suits filenames when more than one extension is accepted. For a marker inside a larger value, use start and end to isolate the relevant region and use the Boolean result to decide what your code does next.
Compare each input with its expected Boolean result. A single suffix should match, while a different letter case should not.
Keep the input, call, and observed result together as you test a rule. That shows whether the text or range caused a mismatch. Also test a value with no matching ending.
How to use endswith() step by step
Use the suffix argument for one ending, a tuple for alternatives, and positions when the relevant text lies within a larger string.
Step 1: Check one ending
Call the method on the string and pass the exact ending to test. I ran python3 suffix_demo.py and got these results in order:
text = "report.final.csv"
print(text.endswith(".csv"))
print(text.endswith((".csv", ".tsv")))
print(text.endswith("final", 0, 12))
print(text.endswith(".CSV"))
print("report".endswith(""))
Run it with python3 suffix_demo.py. The exact output is:
True
True
True
False
True

The bounded check succeeds because positions 0 through 11 include “final”. The case-mismatched check is False, while the empty suffix check is True.
Step 2: Check several suffixes with a tuple
The tuple example returns True because the filename ends in .jpeg.
filename = "photo.jpeg"
print(filename.endswith((".jpg", ".jpeg")))
This tuple check returns:
True

The tuple is one suffix argument, and either member can match. The demonstration tests .csv against .csv and .tsv.
Step 3: Limit the check with start and end
Provide start as the second argument and end as the third to check a slice without creating a new substring. The end index is exclusive, as it is in ordinary Python slices.
record = "ID:AB-204;"
record.endswith("204", 3, 9)
This tests the characters at positions 3 through 8. Negative positions count from the end, as they do in slices, so use offsets that make the intended range clear.
Edge cases and fixes
Tuple syntax, case differences, empty strings and argument positions account for common surprises with endswith(). Choose the argument form that expresses the check rather than treating later arguments as extra suffixes.
| Case | Symptom | Fix |
|---|---|---|
| Several endings passed separately | TypeError about slice indices | Pass a tuple as the first argument |
| Suffix letter case differs | False result | Normalize case only when the rule ignores case |
| Empty suffix | True at a valid boundary | Reject empty input if a suffix is required |
| List of suffixes | TypeError | Convert the collection to a tuple |
An empty suffix can produce True even though it checks no visible characters. Validate a suffix supplied by optional input when an empty value should not count as a match. For dataframes, pandas.Series.str.endswith() is a separate vectorized interface with its own missing-value option.
Conclusion
Use str.endswith() for a clear Boolean check at the end of a string. A tuple handles alternatives, and start/end confine the check to a range.
For exact argument behavior, see the Python str.endswith() documentation. Related AskPython guides cover Python startswith() and checking whether a string contains a substring.
FAQ
These answers cover common choices when testing string endings.
What does endswith() return?
It returns True when the string ends with the supplied suffix and False otherwise.
How do I check multiple suffixes in Python?
Pass a tuple of suffix strings as the first argument to endswith().
Is Python endswith() case-sensitive?
Yes. The suffix must match letter case unless you normalize the strings first.


