str.endswith
Check whether the string ends with the given suffix.
Python 3.13
str.endswith(suffix[, start[, end]])- [ ]optional part
- suffix
- The expected ending of the string; with a tuple, any of the options matches.str | tuple
- start
- The index where the check starts, 0 by default.intOptional
- end
- The index before which the check stops, the end of the string by default.intOptional
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.
