Python String endswith()

Python String endswith() method

Python String endswith() method is used to check if the given string ends with a specific value.

Consider that given string is in x.

x = "hello world"

Then the return value of x.endswith(“world”) is

True

Since the given string ends with specified value, endswith() method returns True.

In this tutorial, you will learn about String endswith() method, its syntax, and how to use this method in Python programs, with examples.

Syntax of endswith() method

The syntax of String endswith() method in Python is given below.

string.endswith(value, start, end)

Parameters

The endswith() method takes three parameters.

ParameterDescription
valueRequired
A string value.
The value which has to be checked if the given string ends with.
startOptional
An integer value.
Specifies the starting index, from where to start the search for specified value in the string.
The default value is 0.
endOptional
Can be specified only if start is specified.
An integer value.
Specifies the ending index, where to stop the search for specified value in the string.
The default value is end of the string.

Return value

The string endswith() method returns a boolean value of True if the string ends with specified value. Or else, endswith() method returns False.

endswith() method searches the value in the given string from left to right, i.e., from starting of the string to ending of the string, or from specified start index to specified end index.

Examples

1. endswith(value) – Check if the string ends with specified value in Python

In the following program, we take a string in variable x, and check if this string ends with the value “world” using String endswith() method.

We shall use a Python if else statement, and use the endswith() returned value as if-condition.

Python Program

x = "hello world"

if x.endswith("world"):
    print("The string ENDS with specified value.")
else:
    print("The string DOES NOT END with specified value.")
Run Code Copy

Output

The string ENDS with specified value.

Now, let us take a value such that the string does not end with this value.

Python Program

x = "hello world"

if x.endswith("user"):
    print("The string ENDS with specified value.")
else:
    print("The string DOES NOT END with specified value.")
Run Code Copy

Output

The string DOES NOT END with specified value.

2. endswith(value, start)

In the following program, we take a string in variable x, and check if this string ends with the value “world”, where search begins from a specific start index of 3, using String endswith() method.

Call endswith() method on the string, and pass the value and start index as arguments.

We shall use a Python if else statement, with endswith() returned value as if-condition.

Python Program

x = "hello world"

if x.endswith("world", 3):
    print("The string ENDS with specified value.")
else:
    print("The string DOES NOT END with specified value.")
Run Code Copy

Output

The string ENDS with specified value.

3. endswith(value, start, end)

In the following program, we take a string in variable x, and check if this string ends with specific value in the bounds specified by a start index of 0 and end index of 11 using String endswith() method.

Python Program

x = "Some apples are red."

if x.endswith("apples", 0, 11):
    print("The string ENDS with specified value.")
else:
    print("The string DOES NOT END with specified value.")
Run Code Copy

Output

The string ENDS with specified value.

Explanation

 Some apples are red.
 ↑          ↑
 start      end
 Some apples
 
      apples 

Summary

In this tutorial of Python String Methods, we learned about String endswith() method, its syntax, and examples covering scenarios of different combinations of arguments.

Related Tutorials

Code copied to clipboard successfully 👍