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.

Syntax

string.endswith(suffix, start, end)

Arguments

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.

Example

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

# app.py

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

# app.py

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.

False
False

Python string endswith() with tuple suffix

# app.py

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

print(result)

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

print(result)

See the output.

➜  pyt python3 app.py
False
True
➜  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.

# app.py

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

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

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

file_name.endswith(tuple(extensions))

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

➜  pyt python3 app.py
True
➜  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.

# app.py

import re

string = 'eleven11'
m = re.search(r'\d+$', string)

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

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 app.py
11
➜  pyt

Conclusion

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.