Python String endswith Method

The endswith method in Python determines whether a given string terminates with a specified substring, returning true if it does and false if it does not.

Python String endswith Method Syntax

It has the following syntax:

Example

endswith(suffix[, start[, end]])

Parameters

  • suffix : a substring
  • start : start index of a range
  • end : last index of the range

Start and end both parameters are optional.

Return Type

It returns a boolean value either True or False.

Different Examples for Python String endswith Method

Let's examine a few examples to gain insights into the functionality of the endswith method.

Python String endswith Method Example 1

An uncomplicated illustration that yields true since it concludes with a period (.).

Example

# Python endswith() function example

# Variable declaration

str = "Hello this is logicpractice."

isends = str.endswith(".")

# Displaying result

print(isends)

Output:

Python String endswith Method Example 2

It yields a false result because the string does not conclude with "is."

Example

# Python endswith() function example

# Variable declaration

str = "Hello this is logicpractice."

isends = str.endswith("is")

# Displaying result

print(isends)

Output:

Python String endswith Method Example 3

In this instance, we are specifying the initial index of the range from which the method begins its search.

Example

# Python endswith() function example

# Variable declaration

str = "Hello this is logicpractice."

isends = str.endswith("is",10)

# Displaying result

print(isends)

Output:

Python String endswith Method Example 4

It returns true because the third parameter halted the method's execution at index 13.

Example

# Python endswith() function example

# Variable declaration

str = "Hello this is logicpractice."

isends = str.endswith("is",0,13)

# Displaying result

print(isends)

Output:

Input Required

This code uses input(). Please provide values below: