Python String endswith() Method

You are Here:

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)

Output

True

Syntax

txt.endswith(value, start, end)

Parameter Values

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

Return Value

ValueExplanation
TrueIf the specified string matches the end of the given string.
FalseIf 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)

Output

True

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.

We are working to cover every Single Concept in Python.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author