Python String endswith() Method
The Python string endswith() method checks whether a string ends with specific text.
It is useful when you want a simple True or False answer. Beginners often use it for file extensions, URL endings, and checking user input.
Quick example
filename = "report.csv"
print(filename.endswith(".csv"))
print(filename.endswith(".txt"))
Output:
True
False
Use endswith() when you want True or False based on the ending of a string.
What endswith() does
endswith():
- Checks whether a string ends with a given suffix
- Returns
TrueorFalse - Does not change the original string
- Works well with normal text such as file names, URLs, and sentence endings
Example:
text = "hello world"
print(text.endswith("world"))
print(text.endswith("hello"))
Output:
True
False
If you are learning string basics, see Python strings explained: basics and examples.
Basic syntax
string.endswith(suffix, start, end)
suffixis requiredstartandendare optionalstartandendlimit which part of the string is checked
Basic example:
filename = "notes.txt"
print(filename.endswith(".txt"))
Output:
True
Arguments explained
suffix
suffix can be:
- A single string
- A tuple of strings
Single suffix:
name = "image.png"
print(name.endswith(".png"))
Output:
True
Tuple of suffixes:
name = "image.png"
print(name.endswith((".jpg", ".png", ".gif")))
Output:
True
start
start tells Python where to begin checking in the string.
text = "my_report.csv"
print(text.endswith(".csv", 3))
Output:
True
Python checks the part from index 3 onward, which is "report.csv".
end
end tells Python where to stop checking before the end of the full string.
text = "report.csv backup"
print(text.endswith(".csv", 0, 10))
Output:
True
Here, Python only checks "report.csv" and ignores the rest.
Return value
endswith() returns:
Trueif the string ends with the suffixFalseif it does not
This makes it very useful inside if statements.
filename = "data.csv"
if filename.endswith(".csv"):
print("This is a CSV file.")
else:
print("This is not a CSV file.")
Output:
This is a CSV file.
Common beginner examples
Check a file extension
filename = "sales.csv"
print(filename.endswith(".csv"))
Output:
True
Check whether a URL ends with a slash
url = "https://example.com/docs/"
print(url.endswith("/"))
Output:
True
Check whether user input ends with punctuation
message = "Hello!"
print(message.endswith("!"))
Output:
True
Check multiple allowed endings
filename = "photo.jpg"
print(filename.endswith((".jpg", ".png", ".gif")))
Output:
True
If you need to check whether text appears anywhere in a string, not just at the end, see how to check if a string contains a substring in Python.
Case sensitivity
endswith() is case-sensitive.
That means ".CSV" and ".csv" are different.
filename = "REPORT.CSV"
print(filename.endswith(".csv"))
print(filename.endswith(".CSV"))
Output:
False
True
If you want a case-insensitive check, convert the string first with lower().
filename = "REPORT.CSV"
print(filename.lower().endswith(".csv"))
Output:
True
Using multiple suffixes
You can pass a tuple of suffixes to check several endings at once.
filename = "picture.gif"
print(filename.endswith((".jpg", ".png", ".gif")))
Output:
True
This is helpful when you want to allow several file types.
Another example:
document = "notes.txt"
allowed = (".txt", ".md", ".docx")
print(document.endswith(allowed))
Output:
True
If you want the opposite check at the beginning of a string, see Python string startswith() method.
Common mistakes
Here are some common beginner mistakes with endswith().
Using a list instead of a tuple
This is wrong:
filename = "photo.jpg"
# print(filename.endswith([".jpg", ".png"])) # TypeError
Use a tuple instead:
filename = "photo.jpg"
print(filename.endswith((".jpg", ".png")))
Forgetting that the check is case-sensitive
filename = "REPORT.CSV"
print(filename.endswith(".csv"))
Output:
False
Fix:
filename = "REPORT.CSV"
print(filename.lower().endswith(".csv"))
Confusing endswith() with in
endswith() checks only the end of the string.
text = "my_report.csv.backup"
print(text.endswith(".csv"))
print(".csv" in text)
Output:
False
True
Expecting endswith() to remove the suffix
endswith() only checks. It does not edit the string.
filename = "report.csv"
print(filename.endswith(".csv"))
print(filename)
Output:
True
report.csv
If you need to change text, see how to replace text in a string in Python.
Common causes of problems include:
- Passing the wrong suffix because of uppercase vs lowercase text
- Using a list instead of a tuple for multiple endings
- Checking the full string when only part of the string should be checked
- Using
endswith()whenreplace()orstrip()is actually needed
Helpful debugging checks:
print(text)
print(repr(text))
print(text.endswith('.csv'))
print(text.lower().endswith('.csv'))
print(type(text))
FAQ
Does endswith() change the string?
No. It only checks the ending and returns True or False.
Can endswith() check more than one ending?
Yes. Pass a tuple of suffixes, such as (".jpg", ".png").
Is endswith() case-sensitive?
Yes. Use lower() or upper() first if you want a case-insensitive check.
What is the difference between endswith() and find()?
endswith() only checks the end of the string. find() searches for text anywhere in the string.