Endswith function in Python string

In this Python tutorial, I will show you what is endswith function in Python string, its syntax, parameter, and return values. We will also see how the Python endswith() method works with demonstrative examples.

Python, a versatile and widely used programming language, boasts a rich set of built-in functions and methods that help developers perform tasks with ease. One such string method is endswith().

What is the endswith function in Python string

The endswith() method is a built-in function in Python’s string class. It returns True if the string ends with the specified value, otherwise False.

Syntax:

The syntax of the Python string endswith() is:

str.endswith(suffix, start, end)

Here, str is the Python string on which the operation is going to happen.

Parameter:

  • suffix: This can be a single substring or a tuple of suffixes to look for.
  • start (optional): The start position in the Python string where the search should begin.
  • end (optional): The end position in the Python string where the search should terminate.

Return value:

The string endswith() method in Python returns a Boolean value:

  • True: If the string ends with the specified suffix (or one of the specified suffixes, if a tuple of suffixes is provided).
  • False: otherwise.

The Python endswith() string method examples

The endswith string method can be used in many different ways. Let’s explore them one by one.

Example-1: Python endswith() method with a substring

Imagine we are handling a dataset containing addresses from all over the USA in Python. We might need to verify if a given address is in a particular state by checking the state abbreviation at the end of the Python string:

address = "123 Main St, Springfield, IL"

if address.endswith("IL"):
    print("The address is in Illinois.")
else:
    print("The address is not in Illinois.")

The output is: Here, if the endswith() method returns True then, the if statement will be executed otherwise else statement.

The address is in Illinois.
Endswith function in Python string with string as parameter

This way we can check the suffix of the string using endswith function in Python string.

Example-2: The endswith Python method with a tuple of strings

If we have to check multiple substrings we can use a Python tuple to store those values and then use the string endswith() method.

In the context of the American film industry, we might want to check whether a file is in a specific format, such as .mp4 or .avi or .MOV:

file_name = "movie_usa.MOV"

if file_name.endswith((".mp4", ".avi", ".MOV")):
    print("The file is a valid video format.")
else:
    print("The file is not a valid video format.")

The output is:

The file is a valid video format.
Python string endswith methods with tuple of strings

This way we can filter out some specific data with suffixes stored in Python tuples with endswith() string method.

Example-3: String endswith Python method with start and end parameters

Suppose we have a URL, and we want to check if the domain part ends with “.com“. We know that the domain starts after https:// and ends before the first / after that. We can use the Python endswith() method with start and end parameters to achieve this.

url = "https://www.example.com/page1"

start_position = len("https://")
end_position = url.find("/", start_position)

if url.endswith(".com", start_position, end_position):
    print("The domain ends with .com.")
else:
    print("The domain does not end with .com.")

Here, Firstly, we determine where the domain starts in the URL. We know it starts right after https://. And next, we find where the domain ends. The domain ends right before the first / that appears after the protocol.

We can use the Python find() method to find this / as the Python find() function will return the index of the first occurrence of the character. Here, it will search after the start point(as mentioned).

The output is:

The domain ends with .com.
string endswith method with start and end parameter

This way we can use the Python endwith() string method with start and end parameters to find substring within a range.

Conclusion

The endswith() function is a powerful and efficient method in Python’s string arsenal. It provides an easy and direct way to verify the suffix of strings. We have seen what is Endswith function in Python string is, its syntax, parameters, and return values and we have also seen many illustrative examples to demonstrate the functionality of the string endswith() method.

You may also like to read: