Latest Code Tutorials

Python String endswith() Method Example


Python string endswith() is an inbuilt method that is used with strings, and the method returns true if the end of the string with the specified suffix in all other cases it returns false. If the start and end index is not provided, then by default, it takes 0 and length-1 as starting and ending indexes where ending index is not included in our search.

Python String endswith()

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.

#Programs on endswith() Method

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 suffix in the endswith() method

See the following program.


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

See the following code.


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. 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.’ This operation can be computationally expensive.

For more information, read this fantastic article.

#Python string endswith list

If you want to check if the string ends with one of the strings from a list, then you can use the Python endswith() function.

See the following code.


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

Now, for this kind of requirement where you need to check if the last characters in the string are numbers and return those numbers, then you need to use Python regexp.

See the following code.


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, then it will return the number; otherwise, it won’t return.

See the output.

➜  pyt python3
➜  pyt

Finally, Python String endswith() Method Example is over.

Recommended Posts

Python String contains

Python String isupper islower upper lower functions

Python String startswith()

Python String join()

Python String index()

Leave A Reply

Your email address will not be published.

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