Python String endswith Method - Python Tutorial | Logic Practice
Python Course / Data Types / Python String endswith Method

Python String endswith Method

BLUF: This lesson on Python String endswith Method provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python String endswith Method

Mastering Python String endswith Method is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience