Python String endswith() Method with Examples

Python string endswith() method 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.

Content Overview

1 Python String endswith()
1.1 Syntax
1.1.1 Programs on endswith() Method
1.1.2 Write a program including start and end suffix in the endswith() method
2 Python string endswith() With Tuple Suffix
3 Regular expression with Python endswith()
4 Python string endswith list
4.1 How to check if the last characters in a string are numbers

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.

Syntax

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.

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

See the following program.

# 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

See the following code.

# 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. 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.

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.

# 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

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.

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

See the output.

➜  pyt python3 app.py
11
➜  pyt

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

#python

Python String endswith() Method with Examples
1 Likes13.95 GEEK