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
| Name | Description |
|---|---|
| suffix | Text, or a tuple of text choices, to look for at the end. |
| start | Optional starting position. |
| end | Optional 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.