Skip to main content

endswith()

The endswith() method in Python is a string method that returns True if a string ends with a specified suffix, otherwise it returns False. It is used to check if a string ends with a particular substring.

Parameter Values

Parameter Description
suffix

The suffix parameter is a string that specifies the ending to check for in the original string.

start

The start parameter is an optional integer that specifies the starting index within the string where the ending is to be checked.

end

The end parameter is an optional integer that specifies the ending index within the string where the ending is to be checked.

Return Values

The endswith() method returns a bool value: True or False.

How to Use endswith() in Python

Example 1:

The endswith() method returns True if a string ends with the specified suffix, otherwise it returns False.

word = 'Hello World'
print(word.endswith('World'))
Example 2:

The endswith() method can also take a tuple of suffixes as argument to check if the string ends with any of them.

file_name = 'document.txt'
print(file_name.endswith(('.txt', '.pdf')))
Example 3:

The endswith() method is case sensitive by default, but you can use case-insensitive comparison by converting the string to lowercase.

text = 'Python Programming'
print(text.lower().endswith('programming'))