Skip to content

Python String endswith: Check if String Ends With Substring

Python String endswith Check if String Ends With Substring Cover Image

The Python endswith method is used to check if a string ends with a specific substring. In this tutorial, you’ll learn how to use the Python endswith method to evaluate whether a string ends with one or more substrings. Being able to check if a string ends with another string allows you to, for example, check the extension of a file or the type of email domain. Alternatively, you can use the startswithmethod to check if a string starts with a substring.

By the end of this tutorial, you’ll have learned:

  • How to use the Python endswith() function to check if a string ends with a substring
  • How to use the Python endswith() function with multiple strings
  • How to use the Python endswith() function without case sensitivity
  • How to use the Python endswith() function with a list of strings

Understanding the Python endswith Function

Before diving into how to use the Python endsswith() function, it’s important to understand the syntax of the function. The code block below breaks down the different required and optional parameters that the function has to offer.

# Understanding the Python endswith() Function
str.endswith(prefix, start, end)

The table below breaks down the parameters of the function as well as any default arguments that the function provides:

ParameterDescriptionDefault ArgumentAccepted Values
prefix=The suffix to check if a given strings ends withN/Astring or tuple of strings
start=The beginning position to check atNoneinteger
end=Stop comparing string at that positionNoneinteger
Understanding the Python endswith() function

Now that you have a strong understanding of the function, let’s start exploring how you can use it.

Using Python endswith to Check if a String Ends with a Substring

The Python endswith() method checks whether or not a string ends with a substring. The method returns a boolean value: True if the string ends with the provided substrings and False otherwise. The method is case-sensitive – so be careful about the casing you use when you check

Let’s take a look at an example, where we try to and determine whether a filepath is of a certain extension.

# Using Python startswith() to Check if a String Starts with a Substring
text = 'datagy.xlsx'

print('text ends with "X": ', text.endswith('X'))
print('text ends with "x": ', text.endswith('x'))

# Returns:
# text ends with "X":  False
# text ends with "x":  True

We can see from the example above that when we check whether or not the string starts with 'X' and 'x'. The first returns True, while the second returns False.

Similarly, we can use the start= parameter to change the starting position of where to check. Because Python strings are 0-based indexed, we can use these index positions. Let’s start checking if the string’s second letter starts with an 'e':

# Modifying the Start Position of the startswith() Function
text = 'datagy.xlsx'
print(text.endswith('xlsx', -4))

# Returns: True

We can see that the Python .endswith() method supports starting using a negative index. In this example, we used the position of -4, since we knew we wanted to check an for an extension with four characters.

In the following section, you’ll learn how to check if a string ends with multiple different substrings.

Using Python endswith to Check if a String Starts with Multiple Substrings

There may be times when you want to check if a string ends with any of a set of substrings. This means that if you’re looking for, for example, different file extensions.

In order to do this, you can simply pass a tuple of strings into the .endswith() method. This will check whether the string ends with any of the options. Let’s see what this looks like with Python:

# Checking if a String Ends With Multiple Different Substrings
text = 'datagy.xlsx'
print(text.endswith(('xls', 'xlsx')))

# Returns: True

In the code block above, we passed a tuple two different strings into the method. This allows us to check whether the string ends with a provided extension. In this case, the method returned true, indicating that the string has one of the two extensions.

Using Python endswith to Check if a String Ends with a Substring Case Insensitive

In this section, you’ll learn how to use the Python .endswith() method to check if a string starts with a substring using case insensitivity. By default, the method is case-sensitive and there isn’t a parameter to change this.

Because of this, we have two options to use the .endswith() method with case insensitivity:

  1. Passing in all options of case sensitivity
  2. Transforming the string to lowercase

Let’s take a look at the first option:

# Checking if a String Ends With Substring Using Case Insensitivity
text = 'datagy.xlsx'
print(text.endswith(('X', 'x')))

# Returns: True

In the code block above, we checked whether the string ends with a lowercase or uppercase 'x', by passing in a tuple of the two letters. This method works when there are fewer combinations of options, but it would be exhaustive otherwise.

Alternatively, we can represent the string in its lowercase equivalent and check for a lowercase substring. Let’s see what this looks like:

# Checking if a String Starts With Substring Using Case Insensitivity
text = 'datagy.xlsx'
print(text.lower().endswith(('x')))

# Returns: True

In the code block above, we first use the .lower() method to represent the string in lowercase. Then, we can use the .endswith() method to check if a string ends with a pattern using case insensitivity.

Using Python endswith to Check if a String Does Not Ends with a Substring

In this final section, you’ll learn how to use the Python endswith() method to check whether or not a string doesn’t end with a given string. This can be particularly helpful when you want to ensure that a file isn’t using a certain extension.

# Check if a String Doesn't End with a Substring
text = 'datagy.csv'
print(not text.endswith(('csv')))

# Returns: False

In the code block above, we preface calling the .endswith() method with the not operator. This returns the inverse boolean of what the method returns. In the example above, we check whether the string ends with 'csv' (which it does). However, because we apply not, the program returns False.

Conclusion

In this post, you learned how to use the Python .endswith() method to check whether or not a string ends with a particular substring. You first learned how the method works by exploring its parameters and default arguments. Then, you learned how to use the method to check whether a string ends with a particular substring as well as multiple different substrings.

Then, you learned how to check for strings starting with a substring with case insensitivity. Finally, you learned whether or not a string doesn’t end with a substring.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

2 thoughts on “Python String endswith: Check if String Ends With Substring”

Leave a Reply

Your email address will not be published. Required fields are marked *