Python Function Reference

str.endswith

Check whether the string ends with the given suffix.

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

Parameters

suffixstr | tuple
The expected ending of the string; with a tuple, any of the options matches.
startintoptional
The index where the check starts.
endintoptional
The index before which the check stops.

Return value

True or False.

Examples

Checking the end of a string

Python 3.13
print('hello'.endswith('lo'))
True

Checking a file extension

Python 3.13
print('photo.png'.endswith(('.png', '.jpg')))
True

Details

Pass several options as a tuple: a list raises TypeError. The check is case-sensitive, so "photo.PNG".endswith(".png") gives False; for file extensions, convert the name to lowercase first.

See also

Where to learn