Home / Python / Reference / String methods

Python string endswith() method

Check whether a string ends with some text.

Syntax

str.endswith(suffix)

The usual way to check a file type. Give a tuple for several options.

Parameters

suffixa string, or a tuple of strings

Return value

bool

Example

Code

files = ["a.csv", "b.txt", "c.CSV", "d.json"]
print([f for f in files if f.lower().endswith(".csv")])
print("photo.jpeg".endswith((".jpg", ".jpeg", ".png")))

Output

['a.csv', 'c.CSV']
True

Related

Learn it step by step in the free lesson Strings, with exercises that check your answers.