Python String endswith: How to Check If String Ends with Substring

To check if a string starts with another string in Python, use the string startswith() method.

Python String endswith

Python string endswith() is a built-in method that returns True if the end of the string is with the specified suffix. Otherwise, False. If the start and end index is not provided, then by default, it takes 0 and length-1 as starting and ending indexes. It does not include the ending index while in search.

How to Check If String Ends with a Substring

To check if a string ends with a substring in Python, use the string.endswith() function. Python endswith() method returns the boolean:

  1. It returns True if strings end with the specified suffix.
  2. It returns False if a string doesn’t end with a particular suffix.


string.endswith(suffix, start, end)


Suffix: This is the part of the string for which we are checking if the string is ending with that part or not.

Start:  The suffix starts from here; it is optional.

End: The suffix ends here; it is optional.


Write a program to show the working of the endswith() method.


string = "Hello I am a string and I am ending with = end"

result = string.endswith("end")
result1 = string.endswith("END")
print("Comparing it with right suffix so output: ",result)
print("Comparing it with wrong suffix so output: ",result1)

See the output.

Comparing it with right suffix so output:  True
Comparing it with the wrong suffix so output:  False

Write a program including start and end suffixes in the endswith() method


string = "This is a string and is awesome"
right_suf = "some"
wrong_suf = "some!"
print(string.endswith(right_suf, 28, 31))
print(string.endswith(wrong_suf, 28, 31))

See the output.


Python string endswith() with tuple suffix


text = "Jean Milburn"
result = text.endswith(('Jean', 'Otis'))


result = text.endswith(('Jean', 'Milburn'))


See the output.

➜  pyt python3
➜  pyt

Regular expression with Python endswith()

Python endswith() method does not allow for a regular expression. So you can only search for a string suffix. This makes sense.

The regular expression can describe an infinite set of matching strings. For example, the regex ‘.*A’ matches all words ending with the character ‘A.’ However, this operation can be computationally expensive.

Python string endswith list

To check if the string ends with one of the strings from a list, use the Python endswith() function.


extensions = ['.jpg','.png']
file_name = 'test.png'

print(file_name.endswith(('.jpg', '.png')))

Python tuple syntax with endswith() function is the following.


The endswith() function also accepts the tuple. So the code becomes more straightforward. See the output.

➜  pyt python3
➜  pyt

How to check if the last characters in a string are numbers

To check if the last characters in the string are numbers, use regular expressions in Python.


import re

string = 'eleven11'
m ='\d+$', string)

# if the string ends in digits m will be a Match object, or None otherwise.
if m is not None:

In the above example, our string is eleven11. So, if the string contains the number, it will return it; otherwise, it won’t return.

See the output.

➜  pyt python3
➜  pyt


Python String endswith() is a function that checks if a given string ends with a specific suffix. To check if a given string ends with a specific suffix, use the String endswith() function.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.