The endswith() method returns True if a string ends with a specified suffix, and False otherwise. It is one of the most practical string methods for checking file extensions, validating URL endings, and writing clean conditional logic without manual string slicing.
01
Suffix Check
Test how a string ends.
02
Returns bool
True or False result.
03
Tuple Suffixes
Match any of several endings.
04
start / end
Search within a slice.
05
File Types
Check .txt, .pdf, .jpg.
06
vs startswith()
End vs beginning checks.
Fundamentals
Definition and Usage
In Python, endswith() is a built-in string method that checks whether the string’s ending matches a given suffix. For example, "document.txt".endswith(".txt") returns True, while "document.pdf".endswith(".txt") returns False. You can pass a tuple of suffixes to match any one of several endings in a single call.
💡
Beginner Tip
Prefer endswith() over slicing like s[-4:] == ".txt". It is more readable, handles edge cases safely, and supports checking multiple suffixes with a tuple.
Foundation
📝 Syntax
The endswith() method accepts one required and two optional parameters:
python
string.endswith(suffix, start, end)
Syntax Rules
suffix — a string or tuple of strings to check at the end (required).
start — optional start index; the check runs on string[start:end].
end — optional end index; search stops before this position.
Return value — True or False; the original string is not modified.
Case-sensitive — "A" and "a" are treated as different characters.
Cheat Sheet
⚡ Quick Reference
Expression
Result
"hello.py".endswith(".py")
True
"hello.py".endswith(".txt")
False
"photo.jpg".endswith((".jpg", ".png"))
True
"File.TXT".endswith(".txt")
False (case-sensitive)
"abcdef".endswith("ef", 0, 5)
False (slice ends at "e")
Basic
name.endswith(".csv")
Single suffix check
Multiple
name.endswith((".jpg", ".png"))
Any suffix matches
Slice
text.endswith("ing", 0, 7)
Check within range
Negate
not url.endswith("/")
When suffix is absent
Hands-On
Examples Gallery
Run these examples in any Python 3 interpreter. Each one shows a common suffix-checking pattern.
📚 Getting Started
Check whether a filename ends with a specific extension.
Example 1 — Basic endswith()
Verify that a file is a text file by checking its extension.
URL uses HTTPS: True
URL ends with .com: False
Path is PDF: True
How It Works
startswith("https://") checks the URL prefix—the correct way to verify HTTPS.
url.endswith(".com") is False here because the URL ends with "/page", not ".com".
endswith(".pdf") correctly identifies the file extension in the path.
Applications
🚀 Common Use Cases
File extension filters — accept only .csv, .json, or image files in upload handlers.
Path validation — confirm a filepath ends with the expected extension before processing.
URL path checks — detect routes ending in "/" or specific path segments.
Data cleaning — skip or flag rows whose values end with unwanted characters.
Conditional logic — branch program flow based on how a string ends.
🧠 How endswith() Works
1
Python selects the search range
If start and end are given, the check runs on that slice; otherwise the full string is used.
Range
2
Suffix is compared at the end
Python checks whether the final characters of the range match the suffix (or any suffix in a tuple).
Compare
3
True or False is returned
No new string is created—only a boolean answer for your if statement or filter.
Result
=
✅
Clear conditional logic
Readable suffix checks without manual index math or string slicing.
Important
📝 Notes
endswith() is case-sensitive—normalize with .lower() when comparing file extensions.
An empty suffix "" always returns True (every string ends with an empty string).
For complex patterns (wildcards, optional parts), use the re module instead.
Do not use endswith("s") to check HTTPS—use startswith("https://") instead.
Wrap Up
Conclusion
The endswith() method is a simple, readable way to test string endings in Python. Whether you are filtering files by extension, validating paths, or writing conditional logic, it gives you a clear boolean answer in one expression.
Remember: pass a tuple for multiple suffixes, use startswith() for prefixes, and call .lower() when case should not matter.
Use .lower() for case-insensitive extension checks
Prefer endswith() over manual slicing
Use startswith() for URL schemes and path prefixes
❌ Don’t
Use endswith("s") to detect HTTPS URLs
Assume extension checks work across mixed-case filenames without normalizing
Use endswith() for regex-style pattern matching
Forget that endswith(".com") fails when the URL has a path after the domain
Confuse return value—it is bool, not a string or index
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about endswith()
Use these points when checking string endings in Python.
5
Core concepts
✅01
Returns bool
True or False only.
Purpose
📁02
File Extensions
.txt, .pdf, .jpg.
Use case
📝03
Tuple Suffixes
Match any in one call.
Syntax
🔎04
Slice Params
start and end optional.
Advanced
⚠05
Case-Sensitive
Use lower() if needed.
Edge case
❓ Frequently Asked Questions
endswith() returns True if a string ends with the specified suffix, and False otherwise. It is commonly used to check file extensions, URL paths, and string endings.
string.endswith(suffix, start, end). suffix is the ending to check (or a tuple of endings). start and end optionally limit the search to a slice of the string.
Yes. Pass a tuple of suffix strings, such as (".jpg", ".png"). endswith() returns True if the string ends with any suffix in the tuple.
Yes. "File.TXT".endswith(".txt") returns False because uppercase T does not match. Use .lower() on both sides for a case-insensitive check.
endswith() checks the end of a string; startswith() checks the beginning. Use endswith() for file extensions and startswith() for prefixes like "https://".
No. It only reads the string and returns a boolean. The original string stays unchanged.