Python String endswith() method
Python String endswith() method
The string.endswith() method in Python returns a Boolean value True if the string ends with the specified suffix, otherwise it returns False.
Syntax
endswith(suffix[, start[, end]])
Parameter
suffix – This parameter represents a string or tuple of suffixes to be checked.
start (optional) – This parameter signifies the beginning position where suffix is to be checked within the string.
end (optional) - This parameter signifies the ending position where suffix is to be checked within the string.
Return
The method returns a Boolean value True if the strings ends with the specified suffix else it returns False if the string doesn't end with the specified suffix.
Example 1
# Python program explaining
# the endswith() method
# passing the string value
str_val = "This is my Python endswith program. This is a simple method."
# checking whether the string ends with the specified suffix,
result1 = str_val.endswith('simple method.')
# returns False
print(result1)
result2 = str_val.endswith('endswith program.')
# returns True
print(result2)
result3 = str_val.endswith('This is my Python endswith program. This is a simple method.')
# returns True
print(result3)
Output
True False True
Example 2
# Python program explaining
# the endswith() method
strVal = "tutorials and examples."
# start parameter: 10
result1 = strVal.endswith('tutorials.', 10)
print(result1)
# Both start and end is provided
# start: 14, end: 22
# Returns true
result2 = strVal.endswith('examples', 14, 22)
print (result2)
# returns True
result3 = strVal.endswith('tutorials', 0, 9)
print (result3)
Output
False True True
