In this section, we will learn what String endswith() method is and how to use it in Python.
What is String endswith() Method in Python?
The Python string endswith() method is used to see if a string value ends with a specific value or not.
For example, if we want to know a string value ends with “world!”, then we can use this method.
Python String endswith() Method Syntax:
string.endswith(value, start, end)
String endswith() Method Parameter
The method takes 3 arguments:
- The first argument is the value we want to see if the target string ends with it or not.
- The second argument, which is optional, will specify the starting location that the value (the first argument) should be searched for. The default value is set to 0.
- The last argument which again is optional is the index position where the search for the value (the first argument of the method) should stop. The default value is set to the size of the target string.
String endswith() Method Return Value
The return value of this method is of type boolean and will be True if the target string value ends with the specified value (the first argument). Otherwise the value False will return.
Example: using string endswith() method in python
s1 = "They know we know that they know we know!" result = s1.endswith("we know!") print(result)
Output:
True