Python String endswith() Method
The endswith() method determines if the string ends with the specified suffix, and returns True if the string ends with the specified suffix, otherwise it returns False. The optional parameters "start" and "end" are the start and end positions of the retrieved string.
Python String endswith() Syntax
Syntax of the endswith() method.
str.endswith(suffix[, start[, end]])
Python String endswith() Parameters
- suffix – The argument can be a string or an element.
- start – The start position in the string.
- end – The end position in the string.
Python String endswith() Return Value
Returns True if the string contains the specified suffix, False otherwise.
Python String endswith() Example#1
The following example shows an instance of the endswith() method.
#!/usr/bin/python3
Str='Apidemos example....wow!!!'
suffix='!!'
print (Str.endswith(suffix))
print (Str.endswith(suffix,20))
suffix='demo'
print (Str.endswith(suffix))
print (Str.endswith(suffix, 0, 19))
Output:
Python String endswith() Example#2
The endswith() method does not use start and end parameters.
#!/usr/bin/python3
text = "welcome to apidemos.com"
result = text.endswith('apidemos.com')
print (result)
result = text.endswith('welcome')
print (result)
result = text.endswith('welcome to apidemos.com.')
print (result)
Output: