search

str.endswith

Returns True if the string ends with the given suffix, otherwise False. suffix may be a tuple of strings to test multiple candidates at once.

str.endswith(suffix[, start[, end]])

Parameters

Parameter Purpose
suffix string or tuple of strings to test
start position to start testing
end position to stop testing

Examples

>>> 'report.pdf'.endswith('.pdf')
True

simple suffix match

>>> 'photo.jpg'.endswith(('.jpg', '.jpeg', '.png'))
True

tuple tests multiple extensions in one call

>>> 'hello world'.endswith('hello', 0, 5)
True

end index narrows the compared slice

>>> 'FILE.PDF'.endswith('.pdf')
False

case-sensitive

Gotcha

Case-sensitive. For file-extension logic, prefer pathlib.PurePath.suffix which handles paths correctly; since 3.9, str.removesuffix() strips a matched tail.

Related methods

← All Python string methods · Python built-ins