endswith() method in Python

The endswith() method checks if a string ends with the specified suffix.
• The suffix can be a string or a tuple of strings.
• Optional arguments start and end let you restrict the search to a substring.

Syntax string.endswith(suffix, start, end)

Example: Basic usage.

Python

filename = "python_programming.txt"
print(filename.endswith(".txt"))    # True

Example: Using a tuple of suffixes.

Python

files = ["report.pdf", "data.csv", "script.py", "notes.txt"]
for f in files:
    if f.endswith((".csv", ".txt")):   # tuple of suffixes
        print(f)   # prints 'data.csv' and 'notes.txt'

Example: With start and end parameters.

Python

text = "learn_python_basics"
print(text.endswith("python", 6, 12))   # True