In python, the string function endswith() is used to check whether a string ends with another string or not. In this tutorial, we’ll look at its syntax and use-cases along with some examples.
Before we proceed, here’s a quick refresher on python strings – Strings are immutable sequences of unicode characters used to handle textual data. Strings implement all common sequence operations (example, iterations, membership tests, etc). For more, check out our guide on strings and other fundamentals of python.
The endswith() function
As stated above, endswith()
is a string function in python to check whether a string ends with the given suffix or not. The following is its syntax:
Syntax
sample_string.endswith(suffix, start, end)
Here, sample_string is the string you want to check whether it ends with the given suffix or not.
Parameters:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
- suffix: The suffix string to be checked.
- start (optional): The starting position from where the suffix is to be checked within the string.
- end (optional): The ending position till where the suffix is to be checked within the string.
Returns:
The endswith()
function returns:
True
if the string ends with the given suffix.False
if the string doesn’t end with the given suffix.
Examples:
Example 1: Without using the optional parameters
# check if string ends with the suffix
s = "To code or not to code"
# check if s ends with 'to code'
print("Ends with 'to code':", s.endswith('to code'))
# check if the match is case-sensitive or not
print("Ends with 'TO CODE':", s.endswith('TO CODE'))
# check if s ends with 'to'
print("Ends with 'to':", s.endswith('to'))
Output:
Ends with 'to code': True
Ends with 'TO CODE': False
Ends with 'to': False
In the above example, we check whether the string s
ends with different suffixes or not. First, we check for the suffix 'to code'
which returns True
. Then, we check for the same suffix but with all characters as uppercase, 'TO CODE'
which returns False
, this shows that the endswith()
function also takes into account the case of the strings. Finally, we check for the suffix 'to'
which rightfully returns False
.
Example 2: Specifying the start and end parameters in the python string endswith() function.
# check if string ends with the suffix
s = "To code or not to code"
print(s.endswith('code'))
print(s.endswith('code', -4))
print(s.endswith('code', 0, 7))
print(s.endswith('code', 0, 10))
Output:
True
True
True
False
In the above example, we see the results when providing the start and end parameters to the endswith()
string function. To better understand the use of start and end parameters imagine them as the indices to slice a string, like s[start:end]
and then using the suffix to check whether the sliced string ends with it or not.
For more on python string functions, refer to the python docs.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.