Python provides an easy way to find out if a string ends with another smaller string. It does this by providing the endswith()
function. In this tutorial, we will use several examples to show how to make use of Python endswith and python string endswith.
Syntax of Python endswith() method
Python’s endswith()
function checks if a string has another string as a suffix, and returns True if so. The syntax of the function is as follows:
text.endswith(suffix, start, end)
where:
text
is the string to checksuffix
is the string, or tuple of strings, to check for an occurrence at the end oftext
start
(Optional) the index to start the search. Takes a default value of 0end
(Optional) the index to end the search. Takes a default value of -1
endswith()
returns a bool
value telling us whether or not suffix
occurs at the end of text
.
Some examples will help you appreciate python’s endswith()
better.
Find suffix character using Python endswith()
text = "Learn Python with Letstacle"
text.endswith('e') # Returns True
text.endswith('b') # Returns False
python’s endswith()
is case-sensitive. The next example illustrates this.
text = "Learn Python with Letstacle"
text.endswith('E') # Returns False
text.endswith('e') # Returns True
In the above example, the string i.e text ends with a lowercase letter 'e'
, not with an uppercase letter. That is why the first call to ensdwith()
returned False
, while the second call returned True
.
Python endswith() with suffix string
text = "Learn Python with Letstacle"
print(text.endswith('Letstacle')) # returns True
print(text.endswith('LeTsTaClE')) # returns False
The previous code made two calls to endswith(). Since endswith()
is case-sensitive, the second call will fail to find the suffix 'LeTsTaClE'
. So, endswith()
will return False
on the second call.
Matching multiple options using tuples
To test if text
ends with one out of multiple options, use a tuple to specify the options to endswith(). So, instead of specifying a suffix, we specify a tuple of options to check.
text = "Learn Python with Letstacle"
print(text.endswith(('E', 'l'))) # returns False
print(text.endswith(('E', 'e'))) # returns True
The first call failed because none of the options was a suffix to text
. However, the second call to endswith()
returned True
since one of the options is a suffix to text
.
Specifying search range in Python endswith()
So far, we have been searching the whole text for the suffix. It is possible to limit the search using the start and end parameters of python endswith()
.
In the next example, the call to endswith()
returns False
because the suffix 'earn'
does not occur at the end of text
.
# multiple occurrences of 'earn'
# both occur within the string
text = "Learn Python and earn money"
# so trying to match without startposition
print(text.endswith('earn')) # returns False
If we want to make sure endswith()
returns True, we have to set the end argument of the function to -6 (that’s one plus the position where the suffix ends)
# multiple occurrences of 'earn'
# both occur within the string
text = "Learn Python and earn money"
# so trying to match without startposition
print(text.endswith('earn', 0, -6)) # returns True
The last occurrence of ‘earn’ was matched by the example. To match the first occurrence, set the end argument to 5 (which is one plus the position in which the first occurrence of ‘earn’ ends)
# multiple occurrences of 'earn'
# both occur within the string
text = "Learn Python and earn money"
# so trying to match without startposition
print(text.endswith('earn', 0, 5)) # returns True
Conclusion
This ends our tutorial on endswith()
python’s function. Please check out our other articles on python to help improve your knowledge of python.
If you need Python homework help or looking for someone who can python help then feel free to drop us a message.