Python string Method - endswith()
The endswith()
method in Python strings returns True
if the string ends with a specified suffix, and False
otherwise. The suffix is specified as an argument to the endswith()
method.
The syntax for the endswith()
method is as follows:
string.endswith(suffix, start, end)
Here, string
is the string that we want to search, suffix
is the suffix that we want to check for, start
is the starting index for the search (optional, default is 0
), and end
is the ending index for the search (optional, default is the end of the string).
Example:
# Defining a string my_string = "hello world" # Using the endswith() method result = my_string.endswith('world') print(result) # Output: True
In the above example, the endswith()
method is used to check if the string my_string
ends with the suffix 'world'
. Since the string my_string
does end with the suffix 'world'
, the variable result
is assigned the value True
, which is printed using the print()
function. Note that the endswith()
method is case-sensitive, so it will not match the suffix 'WORLD'
or 'world'
if they are in different case.