Python endswith() method returns true of the string ends with the specified substring, otherwise returns false.
Python String endswith() Method Syntax
It has the following syntax:
endswith(suffix[, start[, end]])
Parameters
- suffix : a substring
- start : start index of a range
- end : last index of the range
Start and end both parameters are optional.
Return Type
It returns a boolean value either True or False.
Different Examples for Python String endswith() Method
Let’s see some examples to understand the endswith() method.
Python String endswith() Method Example 1
A simple example which returns true because it ends with dot (.).
# Python endswith() function example
# Variable declaration
str = "Hello this is pythonapp."
isends = str.endswith(".")
# Displaying result
print(isends)
Output:
True
Python String endswith() Method Example 2
It returns false because string does not end with is.
# Python endswith() function example
# Variable declaration
str = "Hello this is pythonapp."
isends = str.endswith("is")
# Displaying result
print(isends)
Output:
False
Python String endswith() Method Example 3
Here, we are providing start index of the range from where method starts searching.
# Python endswith() function example
# Variable declaration
str = "Hello this is pythonapp."
isends = str.endswith("is",10)
# Displaying result
print(isends)
Output:
False
Python String endswith() Method Example 4
It returns true because third parameter stopped the method at index 13.
# Python endswith() function example
# Variable declaration
str = "Hello this is pythonapp."
isends = str.endswith("is",0,13)
# Displaying result
print(isends)
Output:
True
Leave a Reply