Skip to content
String Methods

endswith()

Check whether text ends with a particular suffix.

str.endswith(suffix, start=0, end=len(str))

str.endswith() returns True if the string ends with the specified suffix; otherwise returns False.

The suffix can also be a tuple of string suffixes to look for.

Parameters

NameDescription
suffixText, or a tuple of text choices, to look for at the end.
startOptional starting position.
endOptional position where the check stops.

Returns

True or False.

Try it

Run it and change it
filename = "report.txt"
print(filename.endswith(".txt"))

Worth knowing

  • Tuple of suffixes: Passing a tuple like filename.endswith(('.png', '.jpg', '.gif')) checks for multiple allowed extensions in a single readable call.

Related entries