Python - String endswith() Method
The Python String endswith() method is used to check whether the string ends with specified value or not. It returns true when the string ends with the specified value, else returns false.
Syntax
string.endswith(value, start, end)
Parameters
value |
Required. value to check whether the string ends with or not. |
start |
Optional. An integer specifying start position of search. default value is 0. |
end |
Optional. An integer specifying end position of search. default value is end of the string. |
Return Value
Returns True if the string ends with the specified value, else returns False.
Example:
In the below example, endswith() method is used to check whether the string (or a given section of the string) ends with specified value or not.
MyString = "Hello World!" print(MyString.endswith("World!")) print(MyString.endswith("World!", 6, 12))
The output of the above code will be:
True True
❮ Python String Methods