Python String endswith() Method

Last Updated:

Python String endswith()

The endswith() method checks whether the specified string matches the end of the given string or not.

Example

Python Compiler
txt = "Python is simple." x = txt.endswith("simple.") print(x)

Syntax

txt.endswith(value, start, end)

Parameter Values

ValueTypeExplanation
value Required Specifies the value to check if the string ends with.
start Optional Specifies the index position to start the search.
Default value is 0.
end Optional Specifies the index position to end the search.
Default value is the length of the given string (txt).

Return Value

ValueExplanation
True If the specified string matches the end of the given string.
False If the specified string does not matches the end of the given string.

More Examples

Check if position 2 to 4 ends with the phrase "th":

Note: The endswith() method will include the start index value but exclude the end index value.

In the following example, the index value of 2(t) and the index value of 3(h) is considered but not the index value of 3 (o).

Example

Python Compiler
txt = "Python is simple." x = txt.endswith("th", 2, 4) print(x)

Share this Page

Meet the Author